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