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