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" |
| 25 | #include "llvm/MC/MCSymbol.h" |
| 26 | #include "llvm/Target/Mangler.h" |
| 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Target/TargetOptions.h" |
| 30 | #include "llvm/Support/Dwarf.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/ADT/SmallString.h" |
| 34 | #include "llvm/ADT/StringExtras.h" |
| 35 | using namespace llvm; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 36 | using namespace dwarf; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // ELF |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
| 42 | |
| 43 | TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() { |
| 44 | // If we have the section uniquing map, free it. |
| 45 | delete (ELFUniqueMapTy*)UniquingMap; |
| 46 | } |
| 47 | |
| 48 | const MCSection *TargetLoweringObjectFileELF:: |
| 49 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 50 | SectionKind Kind, bool IsExplicit) const { |
| 51 | if (UniquingMap == 0) |
| 52 | UniquingMap = new ELFUniqueMapTy(); |
| 53 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap; |
| 54 | |
| 55 | // Do the lookup, if we have a hit, return it. |
| 56 | const MCSectionELF *&Entry = Map[Section]; |
| 57 | if (Entry) return Entry; |
| 58 | |
| 59 | return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit, |
| 60 | getContext()); |
| 61 | } |
| 62 | |
| 63 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 64 | const TargetMachine &TM) { |
| 65 | if (UniquingMap != 0) |
| 66 | ((ELFUniqueMapTy*)UniquingMap)->clear(); |
| 67 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 68 | |
| 69 | BSSSection = |
| 70 | getELFSection(".bss", MCSectionELF::SHT_NOBITS, |
| 71 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 72 | SectionKind::getBSS()); |
| 73 | |
| 74 | TextSection = |
| 75 | getELFSection(".text", MCSectionELF::SHT_PROGBITS, |
| 76 | MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC, |
| 77 | SectionKind::getText()); |
| 78 | |
| 79 | DataSection = |
| 80 | getELFSection(".data", MCSectionELF::SHT_PROGBITS, |
| 81 | MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC, |
| 82 | SectionKind::getDataRel()); |
| 83 | |
| 84 | ReadOnlySection = |
| 85 | getELFSection(".rodata", MCSectionELF::SHT_PROGBITS, |
| 86 | MCSectionELF::SHF_ALLOC, |
| 87 | SectionKind::getReadOnly()); |
| 88 | |
| 89 | TLSDataSection = |
| 90 | getELFSection(".tdata", MCSectionELF::SHT_PROGBITS, |
| 91 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 92 | MCSectionELF::SHF_WRITE, SectionKind::getThreadData()); |
| 93 | |
| 94 | TLSBSSSection = |
| 95 | getELFSection(".tbss", MCSectionELF::SHT_NOBITS, |
| 96 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS | |
| 97 | MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS()); |
| 98 | |
| 99 | DataRelSection = |
| 100 | getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS, |
| 101 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 102 | SectionKind::getDataRel()); |
| 103 | |
| 104 | DataRelLocalSection = |
| 105 | getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS, |
| 106 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 107 | SectionKind::getDataRelLocal()); |
| 108 | |
| 109 | DataRelROSection = |
| 110 | getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS, |
| 111 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 112 | SectionKind::getReadOnlyWithRel()); |
| 113 | |
| 114 | DataRelROLocalSection = |
| 115 | getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, |
| 116 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 117 | SectionKind::getReadOnlyWithRelLocal()); |
| 118 | |
| 119 | MergeableConst4Section = |
| 120 | getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS, |
| 121 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 122 | SectionKind::getMergeableConst4()); |
| 123 | |
| 124 | MergeableConst8Section = |
| 125 | getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS, |
| 126 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 127 | SectionKind::getMergeableConst8()); |
| 128 | |
| 129 | MergeableConst16Section = |
| 130 | getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS, |
| 131 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE, |
| 132 | SectionKind::getMergeableConst16()); |
| 133 | |
| 134 | StaticCtorSection = |
| 135 | getELFSection(".ctors", MCSectionELF::SHT_PROGBITS, |
| 136 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 137 | SectionKind::getDataRel()); |
| 138 | |
| 139 | StaticDtorSection = |
| 140 | getELFSection(".dtors", MCSectionELF::SHT_PROGBITS, |
| 141 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 142 | SectionKind::getDataRel()); |
| 143 | |
| 144 | // Exception Handling Sections. |
| 145 | |
| 146 | // FIXME: We're emitting LSDA info into a readonly section on ELF, even though |
| 147 | // it contains relocatable pointers. In PIC mode, this is probably a big |
| 148 | // runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 149 | // adjusted or this should be a data section. |
| 150 | LSDASection = |
| 151 | getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS, |
| 152 | MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly()); |
| 153 | EHFrameSection = |
| 154 | getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS, |
| 155 | MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE, |
| 156 | SectionKind::getDataRel()); |
| 157 | |
| 158 | // Debug Info Sections. |
| 159 | DwarfAbbrevSection = |
| 160 | getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0, |
| 161 | SectionKind::getMetadata()); |
| 162 | DwarfInfoSection = |
| 163 | getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0, |
| 164 | SectionKind::getMetadata()); |
| 165 | DwarfLineSection = |
| 166 | getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0, |
| 167 | SectionKind::getMetadata()); |
| 168 | DwarfFrameSection = |
| 169 | getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0, |
| 170 | SectionKind::getMetadata()); |
| 171 | DwarfPubNamesSection = |
| 172 | getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0, |
| 173 | SectionKind::getMetadata()); |
| 174 | DwarfPubTypesSection = |
| 175 | getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0, |
| 176 | SectionKind::getMetadata()); |
| 177 | DwarfStrSection = |
| 178 | getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0, |
| 179 | SectionKind::getMetadata()); |
| 180 | DwarfLocSection = |
| 181 | getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0, |
| 182 | SectionKind::getMetadata()); |
| 183 | DwarfARangesSection = |
| 184 | getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0, |
| 185 | SectionKind::getMetadata()); |
| 186 | DwarfRangesSection = |
| 187 | getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0, |
| 188 | SectionKind::getMetadata()); |
| 189 | DwarfMacroInfoSection = |
| 190 | getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0, |
| 191 | SectionKind::getMetadata()); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static SectionKind |
| 196 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
| 197 | if (Name.empty() || Name[0] != '.') return K; |
| 198 | |
| 199 | // Some lame default implementation based on some magic section names. |
| 200 | if (Name == ".bss" || |
| 201 | Name.startswith(".bss.") || |
| 202 | Name.startswith(".gnu.linkonce.b.") || |
| 203 | Name.startswith(".llvm.linkonce.b.") || |
| 204 | Name == ".sbss" || |
| 205 | Name.startswith(".sbss.") || |
| 206 | Name.startswith(".gnu.linkonce.sb.") || |
| 207 | Name.startswith(".llvm.linkonce.sb.")) |
| 208 | return SectionKind::getBSS(); |
| 209 | |
| 210 | if (Name == ".tdata" || |
| 211 | Name.startswith(".tdata.") || |
| 212 | Name.startswith(".gnu.linkonce.td.") || |
| 213 | Name.startswith(".llvm.linkonce.td.")) |
| 214 | return SectionKind::getThreadData(); |
| 215 | |
| 216 | if (Name == ".tbss" || |
| 217 | Name.startswith(".tbss.") || |
| 218 | Name.startswith(".gnu.linkonce.tb.") || |
| 219 | Name.startswith(".llvm.linkonce.tb.")) |
| 220 | return SectionKind::getThreadBSS(); |
| 221 | |
| 222 | return K; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
| 227 | |
| 228 | if (Name == ".init_array") |
| 229 | return MCSectionELF::SHT_INIT_ARRAY; |
| 230 | |
| 231 | if (Name == ".fini_array") |
| 232 | return MCSectionELF::SHT_FINI_ARRAY; |
| 233 | |
| 234 | if (Name == ".preinit_array") |
| 235 | return MCSectionELF::SHT_PREINIT_ARRAY; |
| 236 | |
| 237 | if (K.isBSS() || K.isThreadBSS()) |
| 238 | return MCSectionELF::SHT_NOBITS; |
| 239 | |
| 240 | return MCSectionELF::SHT_PROGBITS; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | static unsigned |
| 245 | getELFSectionFlags(SectionKind K) { |
| 246 | unsigned Flags = 0; |
| 247 | |
| 248 | if (!K.isMetadata()) |
| 249 | Flags |= MCSectionELF::SHF_ALLOC; |
| 250 | |
| 251 | if (K.isText()) |
| 252 | Flags |= MCSectionELF::SHF_EXECINSTR; |
| 253 | |
| 254 | if (K.isWriteable()) |
| 255 | Flags |= MCSectionELF::SHF_WRITE; |
| 256 | |
| 257 | if (K.isThreadLocal()) |
| 258 | Flags |= MCSectionELF::SHF_TLS; |
| 259 | |
| 260 | // K.isMergeableConst() is left out to honour PR4650 |
| 261 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 262 | K.isMergeableConst8() || K.isMergeableConst16()) |
| 263 | Flags |= MCSectionELF::SHF_MERGE; |
| 264 | |
| 265 | if (K.isMergeableCString()) |
| 266 | Flags |= MCSectionELF::SHF_STRINGS; |
| 267 | |
| 268 | return Flags; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | const MCSection *TargetLoweringObjectFileELF:: |
| 273 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 274 | Mangler *Mang, const TargetMachine &TM) const { |
| 275 | StringRef SectionName = GV->getSection(); |
| 276 | |
| 277 | // Infer section flags from the section name if we can. |
| 278 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 279 | |
| 280 | return getELFSection(SectionName, |
| 281 | getELFSectionType(SectionName, Kind), |
| 282 | getELFSectionFlags(Kind), Kind, true); |
| 283 | } |
| 284 | |
| 285 | static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 286 | if (Kind.isText()) return ".gnu.linkonce.t."; |
| 287 | if (Kind.isReadOnly()) return ".gnu.linkonce.r."; |
| 288 | |
| 289 | if (Kind.isThreadData()) return ".gnu.linkonce.td."; |
| 290 | if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; |
| 291 | |
| 292 | if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; |
| 293 | if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; |
| 294 | if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; |
| 295 | if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; |
| 296 | |
| 297 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 298 | return ".gnu.linkonce.d.rel.ro."; |
| 299 | } |
| 300 | |
| 301 | const MCSection *TargetLoweringObjectFileELF:: |
| 302 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 303 | Mangler *Mang, const TargetMachine &TM) const { |
| 304 | |
| 305 | // If this global is linkonce/weak and the target handles this by emitting it |
| 306 | // into a 'uniqued' section name, create and return the section now. |
| 307 | if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) { |
| 308 | const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); |
| 309 | SmallString<128> Name; |
| 310 | Name.append(Prefix, Prefix+strlen(Prefix)); |
| 311 | Mang->getNameWithPrefix(Name, GV, false); |
| 312 | return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind), |
| 313 | getELFSectionFlags(Kind), Kind); |
| 314 | } |
| 315 | |
| 316 | if (Kind.isText()) return TextSection; |
| 317 | |
| 318 | if (Kind.isMergeable1ByteCString() || |
| 319 | Kind.isMergeable2ByteCString() || |
| 320 | Kind.isMergeable4ByteCString()) { |
| 321 | |
| 322 | // We also need alignment here. |
| 323 | // FIXME: this is getting the alignment of the character, not the |
| 324 | // alignment of the global! |
| 325 | unsigned Align = |
| 326 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
| 327 | |
| 328 | const char *SizeSpec = ".rodata.str1."; |
| 329 | if (Kind.isMergeable2ByteCString()) |
| 330 | SizeSpec = ".rodata.str2."; |
| 331 | else if (Kind.isMergeable4ByteCString()) |
| 332 | SizeSpec = ".rodata.str4."; |
| 333 | else |
| 334 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 335 | |
| 336 | |
| 337 | std::string Name = SizeSpec + utostr(Align); |
| 338 | return getELFSection(Name, MCSectionELF::SHT_PROGBITS, |
| 339 | MCSectionELF::SHF_ALLOC | |
| 340 | MCSectionELF::SHF_MERGE | |
| 341 | MCSectionELF::SHF_STRINGS, |
| 342 | Kind); |
| 343 | } |
| 344 | |
| 345 | if (Kind.isMergeableConst()) { |
| 346 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 347 | return MergeableConst4Section; |
| 348 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 349 | return MergeableConst8Section; |
| 350 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 351 | return MergeableConst16Section; |
| 352 | return ReadOnlySection; // .const |
| 353 | } |
| 354 | |
| 355 | if (Kind.isReadOnly()) return ReadOnlySection; |
| 356 | |
| 357 | if (Kind.isThreadData()) return TLSDataSection; |
| 358 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 359 | |
| 360 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 361 | // really emitted with the magic .comm directive, which creates a symbol table |
| 362 | // entry but not a section. |
| 363 | if (Kind.isBSS() || Kind.isCommon()) return BSSSection; |
| 364 | |
| 365 | if (Kind.isDataNoRel()) return DataSection; |
| 366 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 367 | if (Kind.isDataRel()) return DataRelSection; |
| 368 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 369 | |
| 370 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 371 | return DataRelROSection; |
| 372 | } |
| 373 | |
| 374 | /// getSectionForConstant - Given a mergeable constant with the |
| 375 | /// specified size and relocation information, return a section that it |
| 376 | /// should be placed in. |
| 377 | const MCSection *TargetLoweringObjectFileELF:: |
| 378 | getSectionForConstant(SectionKind Kind) const { |
| 379 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 380 | return MergeableConst4Section; |
| 381 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 382 | return MergeableConst8Section; |
| 383 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 384 | return MergeableConst16Section; |
| 385 | if (Kind.isReadOnly()) |
| 386 | return ReadOnlySection; |
| 387 | |
| 388 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 389 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 390 | return DataRelROSection; |
| 391 | } |
| 392 | |
| 393 | const MCExpr *TargetLoweringObjectFileELF:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 394 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 395 | MachineModuleInfo *MMI, |
| 396 | unsigned Encoding, MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 397 | |
| 398 | if (Encoding & dwarf::DW_EH_PE_indirect) { |
| 399 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 400 | |
| 401 | SmallString<128> Name; |
| 402 | Mang->getNameWithPrefix(Name, GV, true); |
| 403 | Name += ".DW.stub"; |
| 404 | |
| 405 | // Add information about the stub reference to ELFMMI so that the stub |
| 406 | // gets emitted by the asmprinter. |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 407 | MCSymbol *Sym = getContext().GetOrCreateTemporarySymbol(Name.str()); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 408 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(Sym); |
| 409 | if (StubSym.getPointer() == 0) { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 410 | Name.clear(); |
| 411 | Mang->getNameWithPrefix(Name, GV, false); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 413 | if (GV->hasPrivateLinkage()) |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 414 | StubSym = MachineModuleInfoImpl:: |
| 415 | StubValueTy(getContext().GetOrCreateTemporarySymbol(Name.str()), |
| 416 | false); |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 417 | else |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 418 | StubSym = MachineModuleInfoImpl:: |
| 419 | StubValueTy(getContext().GetOrCreateSymbol(Name.str()), |
| 420 | !GV->hasInternalLinkage()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return TargetLoweringObjectFile:: |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame^] | 424 | getExprForDwarfReference(Sym, Mang, MMI, |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 425 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 429 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | // MachO |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
| 436 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
| 437 | |
| 438 | TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() { |
| 439 | // If we have the MachO uniquing map, free it. |
| 440 | delete (MachOUniqueMapTy*)UniquingMap; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | const MCSectionMachO *TargetLoweringObjectFileMachO:: |
| 445 | getMachOSection(StringRef Segment, StringRef Section, |
| 446 | unsigned TypeAndAttributes, |
| 447 | unsigned Reserved2, SectionKind Kind) const { |
| 448 | // We unique sections by their segment/section pair. The returned section |
| 449 | // may not have the same flags as the requested section, if so this should be |
| 450 | // diagnosed by the client as an error. |
| 451 | |
| 452 | // Create the map if it doesn't already exist. |
| 453 | if (UniquingMap == 0) |
| 454 | UniquingMap = new MachOUniqueMapTy(); |
| 455 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap; |
| 456 | |
| 457 | // Form the name to look up. |
| 458 | SmallString<64> Name; |
| 459 | Name += Segment; |
| 460 | Name.push_back(','); |
| 461 | Name += Section; |
| 462 | |
| 463 | // Do the lookup, if we have a hit, return it. |
| 464 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 465 | if (Entry) return Entry; |
| 466 | |
| 467 | // Otherwise, return a new section. |
| 468 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, |
| 469 | Reserved2, Kind, getContext()); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 474 | const TargetMachine &TM) { |
Chris Lattner | 09d53fe | 2010-03-10 07:20:42 +0000 | [diff] [blame] | 475 | // _foo.eh symbols are currently always exported so that the linker knows |
| 476 | // about them. This is not necessary on 10.6 and later, but it |
| 477 | // doesn't hurt anything. |
| 478 | // FIXME: I need to get this from Triple. |
| 479 | IsFunctionEHSymbolGlobal = true; |
| 480 | IsFunctionEHFrameSymbolPrivate = false; |
| 481 | SupportsWeakOmittedEHFrame = false; |
| 482 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 483 | if (UniquingMap != 0) |
| 484 | ((MachOUniqueMapTy*)UniquingMap)->clear(); |
| 485 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 486 | |
| 487 | TextSection // .text |
| 488 | = getMachOSection("__TEXT", "__text", |
| 489 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 490 | SectionKind::getText()); |
| 491 | DataSection // .data |
| 492 | = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel()); |
| 493 | |
| 494 | CStringSection // .cstring |
| 495 | = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS, |
| 496 | SectionKind::getMergeable1ByteCString()); |
| 497 | UStringSection |
| 498 | = getMachOSection("__TEXT","__ustring", 0, |
| 499 | SectionKind::getMergeable2ByteCString()); |
| 500 | FourByteConstantSection // .literal4 |
| 501 | = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS, |
| 502 | SectionKind::getMergeableConst4()); |
| 503 | EightByteConstantSection // .literal8 |
| 504 | = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS, |
| 505 | SectionKind::getMergeableConst8()); |
| 506 | |
| 507 | // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back |
| 508 | // to using it in -static mode. |
| 509 | SixteenByteConstantSection = 0; |
| 510 | if (TM.getRelocationModel() != Reloc::Static && |
| 511 | TM.getTargetData()->getPointerSize() == 32) |
| 512 | SixteenByteConstantSection = // .literal16 |
| 513 | getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS, |
| 514 | SectionKind::getMergeableConst16()); |
| 515 | |
| 516 | ReadOnlySection // .const |
| 517 | = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); |
| 518 | |
| 519 | TextCoalSection |
| 520 | = getMachOSection("__TEXT", "__textcoal_nt", |
| 521 | MCSectionMachO::S_COALESCED | |
| 522 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 523 | SectionKind::getText()); |
| 524 | ConstTextCoalSection |
| 525 | = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED, |
| 526 | SectionKind::getText()); |
| 527 | ConstDataCoalSection |
| 528 | = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED, |
| 529 | SectionKind::getText()); |
| 530 | ConstDataSection // .const_data |
| 531 | = getMachOSection("__DATA", "__const", 0, |
| 532 | SectionKind::getReadOnlyWithRel()); |
| 533 | DataCoalSection |
| 534 | = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED, |
| 535 | SectionKind::getDataRel()); |
| 536 | DataCommonSection |
| 537 | = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL, |
| 538 | SectionKind::getBSS()); |
| 539 | DataBSSSection |
| 540 | = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL, |
| 541 | SectionKind::getBSS()); |
| 542 | |
| 543 | |
| 544 | LazySymbolPointerSection |
| 545 | = getMachOSection("__DATA", "__la_symbol_ptr", |
| 546 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, |
| 547 | SectionKind::getMetadata()); |
| 548 | NonLazySymbolPointerSection |
| 549 | = getMachOSection("__DATA", "__nl_symbol_ptr", |
| 550 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 551 | SectionKind::getMetadata()); |
| 552 | |
| 553 | if (TM.getRelocationModel() == Reloc::Static) { |
| 554 | StaticCtorSection |
| 555 | = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel()); |
| 556 | StaticDtorSection |
| 557 | = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel()); |
| 558 | } else { |
| 559 | StaticCtorSection |
| 560 | = getMachOSection("__DATA", "__mod_init_func", |
| 561 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, |
| 562 | SectionKind::getDataRel()); |
| 563 | StaticDtorSection |
| 564 | = getMachOSection("__DATA", "__mod_term_func", |
| 565 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, |
| 566 | SectionKind::getDataRel()); |
| 567 | } |
| 568 | |
| 569 | // Exception Handling. |
Bill Wendling | fec8657 | 2010-03-03 19:31:05 +0000 | [diff] [blame] | 570 | LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0, |
| 571 | SectionKind::getDataRel()); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 572 | EHFrameSection = |
| 573 | getMachOSection("__TEXT", "__eh_frame", |
| 574 | MCSectionMachO::S_COALESCED | |
| 575 | MCSectionMachO::S_ATTR_NO_TOC | |
| 576 | MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS | |
| 577 | MCSectionMachO::S_ATTR_LIVE_SUPPORT, |
| 578 | SectionKind::getReadOnly()); |
| 579 | |
| 580 | // Debug Information. |
| 581 | DwarfAbbrevSection = |
| 582 | getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG, |
| 583 | SectionKind::getMetadata()); |
| 584 | DwarfInfoSection = |
| 585 | getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG, |
| 586 | SectionKind::getMetadata()); |
| 587 | DwarfLineSection = |
| 588 | getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG, |
| 589 | SectionKind::getMetadata()); |
| 590 | DwarfFrameSection = |
| 591 | getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG, |
| 592 | SectionKind::getMetadata()); |
| 593 | DwarfPubNamesSection = |
| 594 | getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG, |
| 595 | SectionKind::getMetadata()); |
| 596 | DwarfPubTypesSection = |
| 597 | getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG, |
| 598 | SectionKind::getMetadata()); |
| 599 | DwarfStrSection = |
| 600 | getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG, |
| 601 | SectionKind::getMetadata()); |
| 602 | DwarfLocSection = |
| 603 | getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG, |
| 604 | SectionKind::getMetadata()); |
| 605 | DwarfARangesSection = |
| 606 | getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG, |
| 607 | SectionKind::getMetadata()); |
| 608 | DwarfRangesSection = |
| 609 | getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG, |
| 610 | SectionKind::getMetadata()); |
| 611 | DwarfMacroInfoSection = |
| 612 | getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG, |
| 613 | SectionKind::getMetadata()); |
| 614 | DwarfDebugInlineSection = |
| 615 | getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, |
| 616 | SectionKind::getMetadata()); |
| 617 | } |
| 618 | |
| 619 | const MCSection *TargetLoweringObjectFileMachO:: |
| 620 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 621 | Mangler *Mang, const TargetMachine &TM) const { |
| 622 | // Parse the section specifier and create it if valid. |
| 623 | StringRef Segment, Section; |
| 624 | unsigned TAA, StubSize; |
| 625 | std::string ErrorCode = |
| 626 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
| 627 | TAA, StubSize); |
| 628 | if (!ErrorCode.empty()) { |
| 629 | // If invalid, report the error with llvm_report_error. |
| 630 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 631 | "' has an invalid section specifier '" + GV->getSection()+ |
| 632 | "': " + ErrorCode + "."); |
| 633 | // Fall back to dropping it into the data section. |
| 634 | return DataSection; |
| 635 | } |
| 636 | |
| 637 | // Get the section. |
| 638 | const MCSectionMachO *S = |
| 639 | getMachOSection(Segment, Section, TAA, StubSize, Kind); |
| 640 | |
| 641 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 642 | // If the user declared multiple globals with different section flags, we need |
| 643 | // to reject it here. |
| 644 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
| 645 | // If invalid, report the error with llvm_report_error. |
| 646 | llvm_report_error("Global variable '" + GV->getNameStr() + |
| 647 | "' section type or attributes does not match previous" |
| 648 | " section specifier"); |
| 649 | } |
| 650 | |
| 651 | return S; |
| 652 | } |
| 653 | |
| 654 | const MCSection *TargetLoweringObjectFileMachO:: |
| 655 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 656 | Mangler *Mang, const TargetMachine &TM) const { |
| 657 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
| 658 | |
| 659 | if (Kind.isText()) |
| 660 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
| 661 | |
| 662 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 663 | // or data depending on if it is writable. |
| 664 | if (GV->isWeakForLinker()) { |
| 665 | if (Kind.isReadOnly()) |
| 666 | return ConstTextCoalSection; |
| 667 | return DataCoalSection; |
| 668 | } |
| 669 | |
| 670 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 671 | if (Kind.isMergeable1ByteCString() && |
| 672 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
| 673 | return CStringSection; |
| 674 | |
| 675 | // Do not put 16-bit arrays in the UString section if they have an |
| 676 | // externally visible label, this runs into issues with certain linker |
| 677 | // versions. |
| 678 | if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && |
| 679 | TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
| 680 | return UStringSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 681 | |
| 682 | if (Kind.isMergeableConst()) { |
| 683 | if (Kind.isMergeableConst4()) |
| 684 | return FourByteConstantSection; |
| 685 | if (Kind.isMergeableConst8()) |
| 686 | return EightByteConstantSection; |
| 687 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 688 | return SixteenByteConstantSection; |
| 689 | } |
| 690 | |
| 691 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 692 | // just drop it in .const. |
| 693 | if (Kind.isReadOnly()) |
| 694 | return ReadOnlySection; |
| 695 | |
| 696 | // If this is marked const, put it into a const section. But if the dynamic |
| 697 | // linker needs to write to it, put it in the data segment. |
| 698 | if (Kind.isReadOnlyWithRel()) |
| 699 | return ConstDataSection; |
| 700 | |
| 701 | // Put zero initialized globals with strong external linkage in the |
| 702 | // DATA, __common section with the .zerofill directive. |
| 703 | if (Kind.isBSSExtern()) |
| 704 | return DataCommonSection; |
| 705 | |
| 706 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 707 | // with the .zerofill directive (aka .lcomm). |
| 708 | if (Kind.isBSSLocal()) |
| 709 | return DataBSSSection; |
| 710 | |
| 711 | // Otherwise, just drop the variable in the normal data section. |
| 712 | return DataSection; |
| 713 | } |
| 714 | |
| 715 | const MCSection * |
| 716 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
| 717 | // If this constant requires a relocation, we have to put it in the data |
| 718 | // segment, not in the text segment. |
| 719 | if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) |
| 720 | return ConstDataSection; |
| 721 | |
| 722 | if (Kind.isMergeableConst4()) |
| 723 | return FourByteConstantSection; |
| 724 | if (Kind.isMergeableConst8()) |
| 725 | return EightByteConstantSection; |
| 726 | if (Kind.isMergeableConst16() && SixteenByteConstantSection) |
| 727 | return SixteenByteConstantSection; |
| 728 | return ReadOnlySection; // .const |
| 729 | } |
| 730 | |
| 731 | /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide |
| 732 | /// not to emit the UsedDirective for some symbols in llvm.used. |
| 733 | // FIXME: REMOVE this (rdar://7071300) |
| 734 | bool TargetLoweringObjectFileMachO:: |
| 735 | shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const { |
| 736 | /// On Darwin, internally linked data beginning with "L" or "l" does not have |
| 737 | /// the directive emitted (this occurs in ObjC metadata). |
| 738 | if (!GV) return false; |
| 739 | |
| 740 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
| 741 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
| 742 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 743 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 744 | // this horrible hack can go away. |
| 745 | SmallString<64> Name; |
| 746 | Mang->getNameWithPrefix(Name, GV, false); |
| 747 | if (Name[0] == 'L' || Name[0] == 'l') |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | return true; |
| 752 | } |
| 753 | |
| 754 | const MCExpr *TargetLoweringObjectFileMachO:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 755 | getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, |
| 756 | MachineModuleInfo *MMI, unsigned Encoding, |
| 757 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 758 | // The mach-o version of this method defaults to returning a stub reference. |
| 759 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 760 | if (Encoding & DW_EH_PE_indirect) { |
| 761 | MachineModuleInfoMachO &MachOMMI = |
| 762 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 763 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 764 | SmallString<128> Name; |
| 765 | Mang->getNameWithPrefix(Name, GV, true); |
| 766 | Name += "$non_lazy_ptr"; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 767 | |
| 768 | // Add information about the stub reference to MachOMMI so that the stub |
| 769 | // gets emitted by the asmprinter. |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 770 | MCSymbol *Sym = getContext().GetOrCreateTemporarySymbol(Name.str()); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 771 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Sym); |
| 772 | if (StubSym.getPointer() == 0) { |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 773 | Name.clear(); |
| 774 | Mang->getNameWithPrefix(Name, GV, false); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 776 | if (GV->hasPrivateLinkage()) |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 777 | StubSym = MachineModuleInfoImpl:: |
| 778 | StubValueTy(getContext().GetOrCreateTemporarySymbol(Name.str()), |
| 779 | false); |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 780 | else |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 781 | StubSym = MachineModuleInfoImpl:: |
| 782 | StubValueTy(getContext().GetOrCreateSymbol(Name.str()), |
| 783 | !GV->hasInternalLinkage()); |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 784 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 785 | |
| 786 | return TargetLoweringObjectFile:: |
Chris Lattner | 42263e2 | 2010-03-11 21:55:20 +0000 | [diff] [blame^] | 787 | getExprForDwarfReference(Sym, Mang, MMI, |
| 788 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return TargetLoweringObjectFile:: |
Chris Lattner | 3192d14 | 2010-03-11 19:41:58 +0000 | [diff] [blame] | 792 | getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 795 | unsigned TargetLoweringObjectFileMachO::getPersonalityEncoding() const { |
| 796 | return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 797 | } |
| 798 | |
| 799 | unsigned TargetLoweringObjectFileMachO::getLSDAEncoding() const { |
| 800 | return DW_EH_PE_pcrel; |
| 801 | } |
| 802 | |
| 803 | unsigned TargetLoweringObjectFileMachO::getFDEEncoding() const { |
| 804 | return DW_EH_PE_pcrel; |
| 805 | } |
| 806 | |
| 807 | unsigned TargetLoweringObjectFileMachO::getTTypeEncoding() const { |
Bill Wendling | fec8657 | 2010-03-03 19:31:05 +0000 | [diff] [blame] | 808 | return DW_EH_PE_absptr; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 809 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 810 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 811 | //===----------------------------------------------------------------------===// |
| 812 | // COFF |
| 813 | //===----------------------------------------------------------------------===// |
| 814 | |
| 815 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
| 816 | |
| 817 | TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() { |
| 818 | delete (COFFUniqueMapTy*)UniquingMap; |
| 819 | } |
| 820 | |
| 821 | |
| 822 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 823 | getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const { |
| 824 | // Create the map if it doesn't already exist. |
| 825 | if (UniquingMap == 0) |
| 826 | UniquingMap = new MachOUniqueMapTy(); |
| 827 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap; |
| 828 | |
| 829 | // Do the lookup, if we have a hit, return it. |
| 830 | const MCSectionCOFF *&Entry = Map[Name]; |
| 831 | if (Entry) return Entry; |
| 832 | |
| 833 | return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext()); |
| 834 | } |
| 835 | |
| 836 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 837 | const TargetMachine &TM) { |
| 838 | if (UniquingMap != 0) |
| 839 | ((COFFUniqueMapTy*)UniquingMap)->clear(); |
| 840 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 841 | TextSection = getCOFFSection("\t.text", true, SectionKind::getText()); |
| 842 | DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel()); |
| 843 | StaticCtorSection = |
| 844 | getCOFFSection(".ctors", false, SectionKind::getDataRel()); |
| 845 | StaticDtorSection = |
| 846 | getCOFFSection(".dtors", false, SectionKind::getDataRel()); |
| 847 | |
| 848 | // FIXME: We're emitting LSDA info into a readonly section on COFF, even |
| 849 | // though it contains relocatable pointers. In PIC mode, this is probably a |
| 850 | // big runtime hit for C++ apps. Either the contents of the LSDA need to be |
| 851 | // adjusted or this should be a data section. |
| 852 | LSDASection = |
| 853 | getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly()); |
| 854 | EHFrameSection = |
| 855 | getCOFFSection(".eh_frame", false, SectionKind::getDataRel()); |
| 856 | |
| 857 | // Debug info. |
| 858 | // FIXME: Don't use 'directive' mode here. |
| 859 | DwarfAbbrevSection = |
| 860 | getCOFFSection("\t.section\t.debug_abbrev,\"dr\"", |
| 861 | true, SectionKind::getMetadata()); |
| 862 | DwarfInfoSection = |
| 863 | getCOFFSection("\t.section\t.debug_info,\"dr\"", |
| 864 | true, SectionKind::getMetadata()); |
| 865 | DwarfLineSection = |
| 866 | getCOFFSection("\t.section\t.debug_line,\"dr\"", |
| 867 | true, SectionKind::getMetadata()); |
| 868 | DwarfFrameSection = |
| 869 | getCOFFSection("\t.section\t.debug_frame,\"dr\"", |
| 870 | true, SectionKind::getMetadata()); |
| 871 | DwarfPubNamesSection = |
| 872 | getCOFFSection("\t.section\t.debug_pubnames,\"dr\"", |
| 873 | true, SectionKind::getMetadata()); |
| 874 | DwarfPubTypesSection = |
| 875 | getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"", |
| 876 | true, SectionKind::getMetadata()); |
| 877 | DwarfStrSection = |
| 878 | getCOFFSection("\t.section\t.debug_str,\"dr\"", |
| 879 | true, SectionKind::getMetadata()); |
| 880 | DwarfLocSection = |
| 881 | getCOFFSection("\t.section\t.debug_loc,\"dr\"", |
| 882 | true, SectionKind::getMetadata()); |
| 883 | DwarfARangesSection = |
| 884 | getCOFFSection("\t.section\t.debug_aranges,\"dr\"", |
| 885 | true, SectionKind::getMetadata()); |
| 886 | DwarfRangesSection = |
| 887 | getCOFFSection("\t.section\t.debug_ranges,\"dr\"", |
| 888 | true, SectionKind::getMetadata()); |
| 889 | DwarfMacroInfoSection = |
| 890 | getCOFFSection("\t.section\t.debug_macinfo,\"dr\"", |
| 891 | true, SectionKind::getMetadata()); |
| 892 | } |
| 893 | |
| 894 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 895 | getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, |
| 896 | Mangler *Mang, const TargetMachine &TM) const { |
| 897 | return getCOFFSection(GV->getSection(), false, Kind); |
| 898 | } |
| 899 | |
| 900 | static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { |
| 901 | if (Kind.isText()) |
| 902 | return ".text$linkonce"; |
| 903 | if (Kind.isWriteable()) |
| 904 | return ".data$linkonce"; |
| 905 | return ".rdata$linkonce"; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 910 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 911 | Mangler *Mang, const TargetMachine &TM) const { |
| 912 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); |
| 913 | |
| 914 | // If this global is linkonce/weak and the target handles this by emitting it |
| 915 | // into a 'uniqued' section name, create and return the section now. |
| 916 | if (GV->isWeakForLinker()) { |
| 917 | const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); |
| 918 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
| 919 | Mang->getNameWithPrefix(Name, GV, false); |
| 920 | return getCOFFSection(Name.str(), false, Kind); |
| 921 | } |
| 922 | |
| 923 | if (Kind.isText()) |
| 924 | return getTextSection(); |
| 925 | |
| 926 | return getDataSection(); |
| 927 | } |
| 928 | |