Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===// |
| 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 | |
| 15 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/GlobalVariable.h" |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
| 22 | #include "llvm/MC/MCContext.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCSectionMachO.h" |
| 25 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSectionCOFF.h" |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCStreamer.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbol.h" |
| 29 | #include "llvm/Target/Mangler.h" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 30 | #include "llvm/DataLayout.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/Target/TargetOptions.h" |
| 33 | #include "llvm/Support/Dwarf.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ELF.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
| 37 | #include "llvm/ADT/SmallString.h" |
| 38 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 8048ebe | 2010-09-27 06:44:54 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Triple.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 41 | using namespace dwarf; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // ELF |
| 45 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 47 | MCSymbol * |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 48 | TargetLoweringObjectFileELF::getCFIPersonalitySymbol(const GlobalValue *GV, |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 49 | Mangler *Mang, |
| 50 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 60246a9 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 51 | unsigned Encoding = getPersonalityEncoding(); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 52 | switch (Encoding & 0x70) { |
| 53 | default: |
| 54 | report_fatal_error("We do not support this DWARF encoding yet!"); |
| 55 | case dwarf::DW_EH_PE_absptr: |
| 56 | return Mang->getSymbol(GV); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 57 | case dwarf::DW_EH_PE_pcrel: { |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 58 | return getContext().GetOrCreateSymbol(StringRef("DW.ref.") + |
| 59 | Mang->getSymbol(GV)->getName()); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer, |
| 65 | const TargetMachine &TM, |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 66 | const MCSymbol *Sym) const { |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 67 | SmallString<64> NameData("DW.ref."); |
| 68 | NameData += Sym->getName(); |
| 69 | MCSymbol *Label = getContext().GetOrCreateSymbol(NameData); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 70 | Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); |
| 71 | Streamer.EmitSymbolAttribute(Label, MCSA_Weak); |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 72 | StringRef Prefix = ".data."; |
| 73 | NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 74 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
| 75 | const MCSection *Sec = getContext().getELFSection(NameData, |
| 76 | ELF::SHT_PROGBITS, |
| 77 | Flags, |
| 78 | SectionKind::getDataRel(), |
| 79 | 0, Label->getName()); |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame^] | 80 | unsigned Size = TM.getDataLayout()->getPointerSize(); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 81 | Streamer.SwitchSection(Sec); |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame^] | 82 | Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 83 | Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
David Chisnall | ca5b752 | 2012-02-17 16:05:50 +0000 | [diff] [blame] | 84 | const MCExpr *E = MCConstantExpr::Create(Size, getContext()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 85 | Streamer.EmitELFSize(Label, E); |
| 86 | Streamer.EmitLabel(Label); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 87 | |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 88 | Streamer.EmitSymbolValue(Sym, Size); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 91 | static SectionKind |
| 92 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
Rafael Espindola | e665798 | 2011-05-24 03:10:31 +0000 | [diff] [blame] | 93 | // N.B.: The defaults used in here are no the same ones used in MC. |
| 94 | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
| 95 | // both gas and MC will produce a section with no flags. Given |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 96 | // section(".eh_frame") gcc will produce: |
| 97 | // |
| 98 | // .section .eh_frame,"a",@progbits |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 99 | if (Name.empty() || Name[0] != '.') return K; |
| 100 | |
| 101 | // Some lame default implementation based on some magic section names. |
| 102 | if (Name == ".bss" || |
| 103 | Name.startswith(".bss.") || |
| 104 | Name.startswith(".gnu.linkonce.b.") || |
| 105 | Name.startswith(".llvm.linkonce.b.") || |
| 106 | Name == ".sbss" || |
| 107 | Name.startswith(".sbss.") || |
| 108 | Name.startswith(".gnu.linkonce.sb.") || |
| 109 | Name.startswith(".llvm.linkonce.sb.")) |
| 110 | return SectionKind::getBSS(); |
| 111 | |
| 112 | if (Name == ".tdata" || |
| 113 | Name.startswith(".tdata.") || |
| 114 | Name.startswith(".gnu.linkonce.td.") || |
| 115 | Name.startswith(".llvm.linkonce.td.")) |
| 116 | return SectionKind::getThreadData(); |
| 117 | |
| 118 | if (Name == ".tbss" || |
| 119 | Name.startswith(".tbss.") || |
| 120 | Name.startswith(".gnu.linkonce.tb.") || |
| 121 | Name.startswith(".llvm.linkonce.tb.")) |
| 122 | return SectionKind::getThreadBSS(); |
| 123 | |
| 124 | return K; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
| 129 | |
| 130 | if (Name == ".init_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 131 | return ELF::SHT_INIT_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 132 | |
| 133 | if (Name == ".fini_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 134 | return ELF::SHT_FINI_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 135 | |
| 136 | if (Name == ".preinit_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 137 | return ELF::SHT_PREINIT_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 138 | |
| 139 | if (K.isBSS() || K.isThreadBSS()) |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 140 | return ELF::SHT_NOBITS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 142 | return ELF::SHT_PROGBITS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | static unsigned |
| 147 | getELFSectionFlags(SectionKind K) { |
| 148 | unsigned Flags = 0; |
| 149 | |
| 150 | if (!K.isMetadata()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 151 | Flags |= ELF::SHF_ALLOC; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 152 | |
| 153 | if (K.isText()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 154 | Flags |= ELF::SHF_EXECINSTR; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 155 | |
Rafael Espindola | d846e3f | 2011-06-07 23:26:45 +0000 | [diff] [blame] | 156 | if (K.isWriteable()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 157 | Flags |= ELF::SHF_WRITE; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 158 | |
| 159 | if (K.isThreadLocal()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 160 | Flags |= ELF::SHF_TLS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 161 | |
| 162 | // K.isMergeableConst() is left out to honour PR4650 |
| 163 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 164 | K.isMergeableConst8() || K.isMergeableConst16()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 165 | Flags |= ELF::SHF_MERGE; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 166 | |
| 167 | if (K.isMergeableCString()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 168 | Flags |= ELF::SHF_STRINGS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 169 | |
| 170 | return Flags; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | const MCSection *TargetLoweringObjectFileELF:: |
| 175 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 176 | Mangler *Mang, const TargetMachine &TM) const { |
| 177 | StringRef SectionName = GV->getSection(); |
| 178 | |
| 179 | // Infer section flags from the section name if we can. |
| 180 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 181 | |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 182 | return getContext().getELFSection(SectionName, |
| 183 | getELFSectionType(SectionName, Kind), |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 184 | getELFSectionFlags(Kind), Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 187 | /// getSectionPrefixForGlobal - Return the section prefix name used by options |
| 188 | /// FunctionsSections and DataSections. |
| 189 | static const char *getSectionPrefixForGlobal(SectionKind Kind) { |
| 190 | if (Kind.isText()) return ".text."; |
| 191 | if (Kind.isReadOnly()) return ".rodata."; |
Anton Korobeynikov | 1d2d5a0 | 2012-02-23 10:36:04 +0000 | [diff] [blame] | 192 | if (Kind.isBSS()) return ".bss."; |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 193 | |
| 194 | if (Kind.isThreadData()) return ".tdata."; |
| 195 | if (Kind.isThreadBSS()) return ".tbss."; |
| 196 | |
| 197 | if (Kind.isDataNoRel()) return ".data."; |
| 198 | if (Kind.isDataRelLocal()) return ".data.rel.local."; |
| 199 | if (Kind.isDataRel()) return ".data.rel."; |
| 200 | if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; |
| 201 | |
| 202 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 203 | return ".data.rel.ro."; |
| 204 | } |
| 205 | |
| 206 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 207 | const MCSection *TargetLoweringObjectFileELF:: |
| 208 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 209 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 210 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 211 | // global value to a uniqued section specifically for it. |
| 212 | bool EmitUniquedSection; |
| 213 | if (Kind.isText()) |
| 214 | EmitUniquedSection = TM.getFunctionSections(); |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 215 | else |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 216 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 217 | |
| 218 | // If this global is linkonce/weak and the target handles this by emitting it |
| 219 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 220 | if ((GV->isWeakForLinker() || EmitUniquedSection) && |
Anton Korobeynikov | 1d2d5a0 | 2012-02-23 10:36:04 +0000 | [diff] [blame] | 221 | !Kind.isCommon()) { |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 222 | const char *Prefix; |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 223 | Prefix = getSectionPrefixForGlobal(Kind); |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 225 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
| 226 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 227 | Name.append(Sym->getName().begin(), Sym->getName().end()); |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 228 | StringRef Group = ""; |
| 229 | unsigned Flags = getELFSectionFlags(Kind); |
| 230 | if (GV->isWeakForLinker()) { |
| 231 | Group = Sym->getName(); |
| 232 | Flags |= ELF::SHF_GROUP; |
| 233 | } |
| 234 | |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 235 | return getContext().getELFSection(Name.str(), |
| 236 | getELFSectionType(Name.str(), Kind), |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 237 | Flags, Kind, 0, Group); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (Kind.isText()) return TextSection; |
| 241 | |
| 242 | if (Kind.isMergeable1ByteCString() || |
| 243 | Kind.isMergeable2ByteCString() || |
| 244 | Kind.isMergeable4ByteCString()) { |
| 245 | |
| 246 | // We also need alignment here. |
| 247 | // FIXME: this is getting the alignment of the character, not the |
| 248 | // alignment of the global! |
| 249 | unsigned Align = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 250 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 251 | |
| 252 | const char *SizeSpec = ".rodata.str1."; |
| 253 | if (Kind.isMergeable2ByteCString()) |
| 254 | SizeSpec = ".rodata.str2."; |
| 255 | else if (Kind.isMergeable4ByteCString()) |
| 256 | SizeSpec = ".rodata.str4."; |
| 257 | else |
| 258 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 259 | |
| 260 | |
| 261 | std::string Name = SizeSpec + utostr(Align); |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 262 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 263 | ELF::SHF_ALLOC | |
| 264 | ELF::SHF_MERGE | |
| 265 | ELF::SHF_STRINGS, |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 266 | Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (Kind.isMergeableConst()) { |
| 270 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 271 | return MergeableConst4Section; |
| 272 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 273 | return MergeableConst8Section; |
| 274 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 275 | return MergeableConst16Section; |
| 276 | return ReadOnlySection; // .const |
| 277 | } |
| 278 | |
| 279 | if (Kind.isReadOnly()) return ReadOnlySection; |
| 280 | |
| 281 | if (Kind.isThreadData()) return TLSDataSection; |
| 282 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 283 | |
| 284 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 285 | // really emitted with the magic .comm directive, which creates a symbol table |
| 286 | // entry but not a section. |
| 287 | if (Kind.isBSS() || Kind.isCommon()) return BSSSection; |
| 288 | |
| 289 | if (Kind.isDataNoRel()) return DataSection; |
| 290 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 291 | if (Kind.isDataRel()) return DataRelSection; |
| 292 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 293 | |
| 294 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 295 | return DataRelROSection; |
| 296 | } |
| 297 | |
| 298 | /// getSectionForConstant - Given a mergeable constant with the |
| 299 | /// specified size and relocation information, return a section that it |
| 300 | /// should be placed in. |
| 301 | const MCSection *TargetLoweringObjectFileELF:: |
| 302 | getSectionForConstant(SectionKind Kind) const { |
| 303 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 304 | return MergeableConst4Section; |
| 305 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 306 | return MergeableConst8Section; |
| 307 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 308 | return MergeableConst16Section; |
| 309 | if (Kind.isReadOnly()) |
| 310 | return ReadOnlySection; |
| 311 | |
| 312 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 313 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 314 | return DataRelROSection; |
| 315 | } |
| 316 | |
| 317 | const MCExpr *TargetLoweringObjectFileELF:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 318 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 319 | MachineModuleInfo *MMI, |
| 320 | unsigned Encoding, MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 321 | |
| 322 | if (Encoding & dwarf::DW_EH_PE_indirect) { |
| 323 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 324 | |
| 325 | SmallString<128> Name; |
| 326 | Mang->getNameWithPrefix(Name, GV, true); |
| 327 | Name += ".DW.stub"; |
| 328 | |
| 329 | // Add information about the stub reference to ELFMMI so that the stub |
| 330 | // gets emitted by the asmprinter. |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 331 | MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 332 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 333 | if (StubSym.getPointer() == 0) { |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 334 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 335 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return TargetLoweringObjectFile:: |
Rafael Espindola | 4788c3e | 2011-04-20 03:08:09 +0000 | [diff] [blame] | 339 | getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 343 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 346 | const MCSection * |
| 347 | TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const { |
| 348 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 349 | // numbering. |
| 350 | if (Priority == 65535) |
| 351 | return StaticCtorSection; |
| 352 | |
Rafael Espindola | d6b43a3 | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 353 | if (UseInitArray) { |
| 354 | std::string Name = std::string(".init_array.") + utostr(Priority); |
| 355 | return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY, |
| 356 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 357 | SectionKind::getDataRel()); |
| 358 | } else { |
| 359 | std::string Name = std::string(".ctors.") + utostr(65535 - Priority); |
| 360 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
| 361 | ELF::SHF_ALLOC |ELF::SHF_WRITE, |
| 362 | SectionKind::getDataRel()); |
| 363 | } |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | const MCSection * |
| 367 | TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const { |
| 368 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 369 | // numbering. |
| 370 | if (Priority == 65535) |
| 371 | return StaticDtorSection; |
| 372 | |
Rafael Espindola | d6b43a3 | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 373 | if (UseInitArray) { |
| 374 | std::string Name = std::string(".fini_array.") + utostr(Priority); |
| 375 | return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY, |
| 376 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 377 | SectionKind::getDataRel()); |
| 378 | } else { |
| 379 | std::string Name = std::string(".dtors.") + utostr(65535 - Priority); |
| 380 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
| 381 | ELF::SHF_ALLOC |ELF::SHF_WRITE, |
| 382 | SectionKind::getDataRel()); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
| 388 | UseInitArray = UseInitArray_; |
| 389 | if (!UseInitArray) |
| 390 | return; |
| 391 | |
| 392 | StaticCtorSection = |
| 393 | getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY, |
| 394 | ELF::SHF_WRITE | |
| 395 | ELF::SHF_ALLOC, |
| 396 | SectionKind::getDataRel()); |
| 397 | StaticDtorSection = |
| 398 | getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, |
| 399 | ELF::SHF_WRITE | |
| 400 | ELF::SHF_ALLOC, |
| 401 | SectionKind::getDataRel()); |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// |
| 405 | // MachO |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 408 | /// emitModuleFlags - Emit the module flags that specify the garbage collection |
| 409 | /// information. |
| 410 | void TargetLoweringObjectFileMachO:: |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 411 | emitModuleFlags(MCStreamer &Streamer, |
| 412 | ArrayRef<Module::ModuleFlagEntry> ModuleFlags, |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 413 | Mangler *Mang, const TargetMachine &TM) const { |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 414 | unsigned VersionVal = 0; |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 415 | unsigned ImageInfoFlags = 0; |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 416 | StringRef SectionVal; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 417 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 418 | for (ArrayRef<Module::ModuleFlagEntry>::iterator |
| 419 | i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { |
| 420 | const Module::ModuleFlagEntry &MFE = *i; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 421 | |
| 422 | // Ignore flags with 'Require' behavior. |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 423 | if (MFE.Behavior == Module::Require) |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 424 | continue; |
| 425 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 426 | StringRef Key = MFE.Key->getString(); |
| 427 | Value *Val = MFE.Val; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 428 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 429 | if (Key == "Objective-C Image Info Version") |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 430 | VersionVal = cast<ConstantInt>(Val)->getZExtValue(); |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 431 | else if (Key == "Objective-C Garbage Collection" || |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 432 | Key == "Objective-C GC Only" || |
| 433 | Key == "Objective-C Is Simulated") |
| 434 | ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue(); |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 435 | else if (Key == "Objective-C Image Info Section") |
| 436 | SectionVal = cast<MDString>(Val)->getString(); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 439 | // The section is mandatory. If we don't have it, then we don't have GC info. |
| 440 | if (SectionVal.empty()) return; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 441 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 442 | StringRef Segment, Section; |
| 443 | unsigned TAA = 0, StubSize = 0; |
| 444 | bool TAAParsed; |
| 445 | std::string ErrorCode = |
| 446 | MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, |
| 447 | TAA, TAAParsed, StubSize); |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 448 | if (!ErrorCode.empty()) |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 449 | // If invalid, report the error with report_fatal_error. |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 450 | report_fatal_error("Invalid section specifier '" + Section + "': " + |
| 451 | ErrorCode + "."); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 452 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 453 | // Get the section. |
| 454 | const MCSectionMachO *S = |
| 455 | getContext().getMachOSection(Segment, Section, TAA, StubSize, |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 456 | SectionKind::getDataNoRel()); |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 457 | Streamer.SwitchSection(S); |
| 458 | Streamer.EmitLabel(getContext(). |
| 459 | GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 460 | Streamer.EmitIntValue(VersionVal, 4); |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 461 | Streamer.EmitIntValue(ImageInfoFlags, 4); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 462 | Streamer.AddBlankLine(); |
| 463 | } |
| 464 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 465 | const MCSection *TargetLoweringObjectFileMachO:: |
| 466 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 467 | Mangler *Mang, const TargetMachine &TM) const { |
| 468 | // Parse the section specifier and create it if valid. |
| 469 | StringRef Segment, Section; |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 470 | unsigned TAA = 0, StubSize = 0; |
| 471 | bool TAAParsed; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 472 | std::string ErrorCode = |
| 473 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 474 | TAA, TAAParsed, StubSize); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 475 | if (!ErrorCode.empty()) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 476 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 477 | report_fatal_error("Global variable '" + GV->getName() + |
| 478 | "' has an invalid section specifier '" + |
| 479 | GV->getSection() + "': " + ErrorCode + "."); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // Get the section. |
| 483 | const MCSectionMachO *S = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 484 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 485 | |
Stuart Hastings | 6ad82d8 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 486 | // If TAA wasn't set by ParseSectionSpecifier() above, |
| 487 | // use the value returned by getMachOSection() as a default. |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 488 | if (!TAAParsed) |
Stuart Hastings | 6ad82d8 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 489 | TAA = S->getTypeAndAttributes(); |
| 490 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 491 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 492 | // If the user declared multiple globals with different section flags, we need |
| 493 | // to reject it here. |
| 494 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 495 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 496 | report_fatal_error("Global variable '" + GV->getName() + |
| 497 | "' section type or attributes does not match previous" |
| 498 | " section specifier"); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | return S; |
| 502 | } |
| 503 | |
| 504 | const MCSection *TargetLoweringObjectFileMachO:: |
| 505 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Eric Christopher | 8116ca5 | 2010-05-22 00:10:22 +0000 | [diff] [blame] | 506 | Mangler *Mang, const TargetMachine &TM) const { |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 507 | |
Eric Christopher | 02b46bc | 2010-05-25 21:28:50 +0000 | [diff] [blame] | 508 | // Handle thread local data. |
Eric Christopher | 8116ca5 | 2010-05-22 00:10:22 +0000 | [diff] [blame] | 509 | if (Kind.isThreadBSS()) return TLSBSSSection; |
Eric Christopher | 02b46bc | 2010-05-25 21:28:50 +0000 | [diff] [blame] | 510 | if (Kind.isThreadData()) return TLSDataSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 511 | |
| 512 | if (Kind.isText()) |
| 513 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
| 514 | |
| 515 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 516 | // or data depending on if it is writable. |
| 517 | if (GV->isWeakForLinker()) { |
| 518 | if (Kind.isReadOnly()) |
| 519 | return ConstTextCoalSection; |
| 520 | return DataCoalSection; |
| 521 | } |
| 522 | |
| 523 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 524 | if (Kind.isMergeable1ByteCString() && |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 525 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 526 | return CStringSection; |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 528 | // Do not put 16-bit arrays in the UString section if they have an |
| 529 | // externally visible label, this runs into issues with certain linker |
| 530 | // versions. |
| 531 | if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 532 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 533 | return UStringSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 534 | |
| 535 | if (Kind.isMergeableConst()) { |
| 536 | if (Kind.isMergeableConst4()) |
| 537 | return FourByteConstantSection; |
| 538 | if (Kind.isMergeableConst8()) |
| 539 | return EightByteConstantSection; |
| 540 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 541 | return SixteenByteConstantSection; |
| 542 | } |
| 543 | |
| 544 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 545 | // just drop it in .const. |
| 546 | if (Kind.isReadOnly()) |
| 547 | return ReadOnlySection; |
| 548 | |
| 549 | // If this is marked const, put it into a const section. But if the dynamic |
| 550 | // linker needs to write to it, put it in the data segment. |
| 551 | if (Kind.isReadOnlyWithRel()) |
| 552 | return ConstDataSection; |
| 553 | |
| 554 | // Put zero initialized globals with strong external linkage in the |
| 555 | // DATA, __common section with the .zerofill directive. |
| 556 | if (Kind.isBSSExtern()) |
| 557 | return DataCommonSection; |
| 558 | |
| 559 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 560 | // with the .zerofill directive (aka .lcomm). |
| 561 | if (Kind.isBSSLocal()) |
| 562 | return DataBSSSection; |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 563 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 564 | // Otherwise, just drop the variable in the normal data section. |
| 565 | return DataSection; |
| 566 | } |
| 567 | |
| 568 | const MCSection * |
| 569 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
| 570 | // If this constant requires a relocation, we have to put it in the data |
| 571 | // segment, not in the text segment. |
| 572 | if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) |
| 573 | return ConstDataSection; |
| 574 | |
| 575 | if (Kind.isMergeableConst4()) |
| 576 | return FourByteConstantSection; |
| 577 | if (Kind.isMergeableConst8()) |
| 578 | return EightByteConstantSection; |
| 579 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 580 | return SixteenByteConstantSection; |
| 581 | return ReadOnlySection; // .const |
| 582 | } |
| 583 | |
| 584 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 585 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 586 | // FIXME: REMOVE this (rdar://7071300) |
| 587 | bool TargetLoweringObjectFileMachO:: |
| 588 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 589 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 590 | /// the directive emitted (this occurs in ObjC metadata). |
| 591 | if (!GV) return false; |
| 592 | |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 593 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 594 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 595 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 596 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 597 | // this horrible hack can go away. |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 598 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 599 | if (Sym->getName()[0] == 'L' || Sym->getName()[0] == 'l') |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
| 606 | const MCExpr *TargetLoweringObjectFileMachO:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 607 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 608 | MachineModuleInfo *MMI, unsigned Encoding, |
| 609 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 610 | // The mach-o version of this method defaults to returning a stub reference. |
| 611 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 612 | if (Encoding & DW_EH_PE_indirect) { |
| 613 | MachineModuleInfoMachO &MachOMMI = |
| 614 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 615 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 616 | SmallString<128> Name; |
| 617 | Mang->getNameWithPrefix(Name, GV, true); |
| 618 | Name += "$non_lazy_ptr"; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 619 | |
| 620 | // Add information about the stub reference to MachOMMI so that the stub |
| 621 | // gets emitted by the asmprinter. |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 622 | MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); |
Bill Wendling | 6712154 | 2011-10-24 23:05:43 +0000 | [diff] [blame] | 623 | MachineModuleInfoImpl::StubValueTy &StubSym = |
| 624 | GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : |
| 625 | MachOMMI.getGVStubEntry(SSym); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 626 | if (StubSym.getPointer() == 0) { |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 627 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 628 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 629 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 630 | |
| 631 | return TargetLoweringObjectFile:: |
Rafael Espindola | 4788c3e | 2011-04-20 03:08:09 +0000 | [diff] [blame] | 632 | getExprForDwarfReference(SSym, Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 636 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 639 | MCSymbol *TargetLoweringObjectFileMachO:: |
Rafael Espindola | 60246a9 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 640 | getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang, |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 641 | MachineModuleInfo *MMI) const { |
| 642 | // The mach-o version of this method defaults to returning a stub reference. |
| 643 | MachineModuleInfoMachO &MachOMMI = |
| 644 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 645 | |
| 646 | SmallString<128> Name; |
| 647 | Mang->getNameWithPrefix(Name, GV, true); |
| 648 | Name += "$non_lazy_ptr"; |
| 649 | |
| 650 | // Add information about the stub reference to MachOMMI so that the stub |
| 651 | // gets emitted by the asmprinter. |
| 652 | MCSymbol *SSym = getContext().GetOrCreateSymbol(Name.str()); |
Bill Wendling | d7c2494 | 2011-11-29 01:43:20 +0000 | [diff] [blame] | 653 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 654 | if (StubSym.getPointer() == 0) { |
| 655 | MCSymbol *Sym = Mang->getSymbol(GV); |
| 656 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 657 | } |
| 658 | |
| 659 | return SSym; |
| 660 | } |
| 661 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 662 | //===----------------------------------------------------------------------===// |
| 663 | // COFF |
| 664 | //===----------------------------------------------------------------------===// |
| 665 | |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 666 | static unsigned |
| 667 | getCOFFSectionFlags(SectionKind K) { |
| 668 | unsigned Flags = 0; |
| 669 | |
Anton Korobeynikov | 36335be | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 670 | if (K.isMetadata()) |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 671 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 672 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 673 | else if (K.isText()) |
| 674 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 675 | COFF::IMAGE_SCN_MEM_EXECUTE | |
Michael J. Spencer | 3931b54 | 2010-10-27 18:52:29 +0000 | [diff] [blame] | 676 | COFF::IMAGE_SCN_MEM_READ | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 677 | COFF::IMAGE_SCN_CNT_CODE; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 678 | else if (K.isBSS ()) |
| 679 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 680 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 681 | COFF::IMAGE_SCN_MEM_READ | |
| 682 | COFF::IMAGE_SCN_MEM_WRITE; |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 683 | else if (K.isThreadLocal()) |
| 684 | Flags |= |
| 685 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 686 | COFF::IMAGE_SCN_MEM_READ | |
| 687 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 688 | else if (K.isReadOnly()) |
| 689 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 690 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 691 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 692 | else if (K.isWriteable()) |
| 693 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 694 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 695 | COFF::IMAGE_SCN_MEM_READ | |
| 696 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 697 | |
| 698 | return Flags; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 702 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 703 | Mangler *Mang, const TargetMachine &TM) const { |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 704 | return getContext().getCOFFSection(GV->getSection(), |
| 705 | getCOFFSectionFlags(Kind), |
| 706 | Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 710 | if (Kind.isText()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame] | 711 | return ".text$"; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 712 | if (Kind.isBSS ()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame] | 713 | return ".bss$"; |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 714 | if (Kind.isThreadLocal()) |
| 715 | return ".tls$"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 716 | if (Kind.isWriteable()) |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame] | 717 | return ".data$"; |
| 718 | return ".rdata$"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
| 722 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 723 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 724 | Mangler *Mang, const TargetMachine &TM) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 725 | |
| 726 | // If this global is linkonce/weak and the target handles this by emitting it |
| 727 | // into a 'uniqued' section name, create and return the section now. |
| 728 | if (GV->isWeakForLinker()) { |
| 729 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
| 730 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 731 | MCSymbol *Sym = Mang->getSymbol(GV); |
NAKAMURA Takumi | 3b3b0eb | 2010-10-19 03:24:42 +0000 | [diff] [blame] | 732 | Name.append(Sym->getName().begin() + 1, Sym->getName().end()); |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 733 | |
| 734 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
| 735 | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 736 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 737 | |
| 738 | return getContext().getCOFFSection(Name.str(), Characteristics, |
Anton Korobeynikov | 657985e | 2010-10-08 21:50:04 +0000 | [diff] [blame] | 739 | COFF::IMAGE_COMDAT_SELECT_ANY, Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if (Kind.isText()) |
| 743 | return getTextSection(); |
| 744 | |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 745 | if (Kind.isThreadLocal()) |
| 746 | return getTLSDataSection(); |
| 747 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 748 | return getDataSection(); |
| 749 | } |
| 750 | |