Deleted Added
full compact
TargetLoweringObjectFile.cpp (202878) TargetLoweringObjectFile.cpp (203954)
1//===-- llvm/Target/TargetLoweringObjectFile.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//===----------------------------------------------------------------------===//

--- 5 unchanged lines hidden (view full) ---

14
15#include "llvm/Target/TargetLoweringObjectFile.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
1//===-- llvm/Target/TargetLoweringObjectFile.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//===----------------------------------------------------------------------===//

--- 5 unchanged lines hidden (view full) ---

14
15#include "llvm/Target/TargetLoweringObjectFile.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCSectionMachO.h"
23#include "llvm/MC/MCSectionELF.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/Target/Mangler.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetOptions.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/Target/Mangler.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/ADT/SmallString.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/ADT/SmallString.h"
32#include "llvm/ADT/StringExtras.h"
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// Generic Code
37//===----------------------------------------------------------------------===//
38
39TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
40 TextSection = 0;

--- 243 unchanged lines hidden (view full) ---

284TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
285 if (Kind.isReadOnly() && ReadOnlySection != 0)
286 return ReadOnlySection;
287
288 return DataSection;
289}
290
291/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
31using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
37TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
38 TextSection = 0;

--- 243 unchanged lines hidden (view full) ---

282TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
283 if (Kind.isReadOnly() && ReadOnlySection != 0)
284 return ReadOnlySection;
285
286 return DataSection;
287}
288
289/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
292/// pc-relative reference to the specified global variable from exception
293/// handling information. In addition to the symbol, this returns
294/// by-reference:
295///
296/// IsIndirect - True if the returned symbol is actually a stub that contains
297/// the address of the symbol, false if the symbol is the global itself.
298///
299/// IsPCRel - True if the symbol reference is already pc-relative, false if
300/// the caller needs to subtract off the address of the reference from the
301/// symbol.
302///
290/// reference to the specified global variable from exception
291/// handling information.
303const MCExpr *TargetLoweringObjectFile::
304getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
292const MCExpr *TargetLoweringObjectFile::
293getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
305 MachineModuleInfo *MMI,
306 bool &IsIndirect, bool &IsPCRel) const {
307 // The generic implementation of this just returns a direct reference to the
308 // symbol.
309 IsIndirect = false;
310 IsPCRel = false;
311
294 MachineModuleInfo *MMI, unsigned Encoding) const {
312 // FIXME: Use GetGlobalValueSymbol.
313 SmallString<128> Name;
314 Mang->getNameWithPrefix(Name, GV, false);
295 // FIXME: Use GetGlobalValueSymbol.
296 SmallString<128> Name;
297 Mang->getNameWithPrefix(Name, GV, false);
315 return MCSymbolRefExpr::Create(Name.str(), getContext());
316}
298 const MCSymbol *Sym = getContext().GetOrCreateSymbol(Name.str());
317
299
318
319//===----------------------------------------------------------------------===//
320// ELF
321//===----------------------------------------------------------------------===//
322typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
323
324TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
325 // If we have the section uniquing map, free it.
326 delete (ELFUniqueMapTy*)UniquingMap;
300 return getSymbolForDwarfReference(Sym, MMI, Encoding);
327}
328
301}
302
329const MCSection *TargetLoweringObjectFileELF::
330getELFSection(StringRef Section, unsigned Type, unsigned Flags,
331 SectionKind Kind, bool IsExplicit) const {
332 if (UniquingMap == 0)
333 UniquingMap = new ELFUniqueMapTy();
334 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
303const MCExpr *TargetLoweringObjectFile::
304getSymbolForDwarfReference(const MCSymbol *Sym, MachineModuleInfo *MMI,
305 unsigned Encoding) const {
306 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
335
307
336 // Do the lookup, if we have a hit, return it.
337 const MCSectionELF *&Entry = Map[Section];
338 if (Entry) return Entry;
339
340 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
341 getContext());
342}
343
344void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
345 const TargetMachine &TM) {
346 if (UniquingMap != 0)
347 ((ELFUniqueMapTy*)UniquingMap)->clear();
348 TargetLoweringObjectFile::Initialize(Ctx, TM);
349
350 BSSSection =
351 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
352 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
353 SectionKind::getBSS());
354
355 TextSection =
356 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
357 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
358 SectionKind::getText());
359
360 DataSection =
361 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
362 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
363 SectionKind::getDataRel());
364
365 ReadOnlySection =
366 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
367 MCSectionELF::SHF_ALLOC,
368 SectionKind::getReadOnly());
369
370 TLSDataSection =
371 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
372 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
373 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
374
375 TLSBSSSection =
376 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
377 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
378 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
379
380 DataRelSection =
381 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
382 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
383 SectionKind::getDataRel());
384
385 DataRelLocalSection =
386 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
387 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
388 SectionKind::getDataRelLocal());
389
390 DataRelROSection =
391 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
392 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
393 SectionKind::getReadOnlyWithRel());
394
395 DataRelROLocalSection =
396 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
397 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
398 SectionKind::getReadOnlyWithRelLocal());
399
400 MergeableConst4Section =
401 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
403 SectionKind::getMergeableConst4());
404
405 MergeableConst8Section =
406 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
407 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
408 SectionKind::getMergeableConst8());
409
410 MergeableConst16Section =
411 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
412 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
413 SectionKind::getMergeableConst16());
414
415 StaticCtorSection =
416 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
417 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
418 SectionKind::getDataRel());
419
420 StaticDtorSection =
421 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
422 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
423 SectionKind::getDataRel());
424
425 // Exception Handling Sections.
426
427 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
428 // it contains relocatable pointers. In PIC mode, this is probably a big
429 // runtime hit for C++ apps. Either the contents of the LSDA need to be
430 // adjusted or this should be a data section.
431 LSDASection =
432 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
433 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
434 EHFrameSection =
435 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
436 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
437 SectionKind::getDataRel());
438
439 // Debug Info Sections.
440 DwarfAbbrevSection =
441 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
442 SectionKind::getMetadata());
443 DwarfInfoSection =
444 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
445 SectionKind::getMetadata());
446 DwarfLineSection =
447 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
448 SectionKind::getMetadata());
449 DwarfFrameSection =
450 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
451 SectionKind::getMetadata());
452 DwarfPubNamesSection =
453 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
454 SectionKind::getMetadata());
455 DwarfPubTypesSection =
456 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
457 SectionKind::getMetadata());
458 DwarfStrSection =
459 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
460 SectionKind::getMetadata());
461 DwarfLocSection =
462 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
463 SectionKind::getMetadata());
464 DwarfARangesSection =
465 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
466 SectionKind::getMetadata());
467 DwarfRangesSection =
468 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
469 SectionKind::getMetadata());
470 DwarfMacroInfoSection =
471 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
472 SectionKind::getMetadata());
473}
474
475
476static SectionKind
477getELFKindForNamedSection(StringRef Name, SectionKind K) {
478 if (Name.empty() || Name[0] != '.') return K;
479
480 // Some lame default implementation based on some magic section names.
481 if (Name == ".bss" ||
482 Name.startswith(".bss.") ||
483 Name.startswith(".gnu.linkonce.b.") ||
484 Name.startswith(".llvm.linkonce.b.") ||
485 Name == ".sbss" ||
486 Name.startswith(".sbss.") ||
487 Name.startswith(".gnu.linkonce.sb.") ||
488 Name.startswith(".llvm.linkonce.sb."))
489 return SectionKind::getBSS();
490
491 if (Name == ".tdata" ||
492 Name.startswith(".tdata.") ||
493 Name.startswith(".gnu.linkonce.td.") ||
494 Name.startswith(".llvm.linkonce.td."))
495 return SectionKind::getThreadData();
496
497 if (Name == ".tbss" ||
498 Name.startswith(".tbss.") ||
499 Name.startswith(".gnu.linkonce.tb.") ||
500 Name.startswith(".llvm.linkonce.tb."))
501 return SectionKind::getThreadBSS();
502
503 return K;
504}
505
506
507static unsigned getELFSectionType(StringRef Name, SectionKind K) {
508
509 if (Name == ".init_array")
510 return MCSectionELF::SHT_INIT_ARRAY;
511
512 if (Name == ".fini_array")
513 return MCSectionELF::SHT_FINI_ARRAY;
514
515 if (Name == ".preinit_array")
516 return MCSectionELF::SHT_PREINIT_ARRAY;
517
518 if (K.isBSS() || K.isThreadBSS())
519 return MCSectionELF::SHT_NOBITS;
520
521 return MCSectionELF::SHT_PROGBITS;
522}
523
524
525static unsigned
526getELFSectionFlags(SectionKind K) {
527 unsigned Flags = 0;
528
529 if (!K.isMetadata())
530 Flags |= MCSectionELF::SHF_ALLOC;
531
532 if (K.isText())
533 Flags |= MCSectionELF::SHF_EXECINSTR;
534
535 if (K.isWriteable())
536 Flags |= MCSectionELF::SHF_WRITE;
537
538 if (K.isThreadLocal())
539 Flags |= MCSectionELF::SHF_TLS;
540
541 // K.isMergeableConst() is left out to honour PR4650
542 if (K.isMergeableCString() || K.isMergeableConst4() ||
543 K.isMergeableConst8() || K.isMergeableConst16())
544 Flags |= MCSectionELF::SHF_MERGE;
545
546 if (K.isMergeableCString())
547 Flags |= MCSectionELF::SHF_STRINGS;
548
549 return Flags;
550}
551
552
553const MCSection *TargetLoweringObjectFileELF::
554getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
555 Mangler *Mang, const TargetMachine &TM) const {
556 StringRef SectionName = GV->getSection();
557
558 // Infer section flags from the section name if we can.
559 Kind = getELFKindForNamedSection(SectionName, Kind);
560
561 return getELFSection(SectionName,
562 getELFSectionType(SectionName, Kind),
563 getELFSectionFlags(Kind), Kind, true);
564}
565
566static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
567 if (Kind.isText()) return ".gnu.linkonce.t.";
568 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
569
570 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
571 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
572
573 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
574 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
575 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
576 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
577
578 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
579 return ".gnu.linkonce.d.rel.ro.";
580}
581
582const MCSection *TargetLoweringObjectFileELF::
583SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
584 Mangler *Mang, const TargetMachine &TM) const {
585
586 // If this global is linkonce/weak and the target handles this by emitting it
587 // into a 'uniqued' section name, create and return the section now.
588 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
589 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
590 SmallString<128> Name;
591 Name.append(Prefix, Prefix+strlen(Prefix));
592 Mang->getNameWithPrefix(Name, GV, false);
593 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
594 getELFSectionFlags(Kind), Kind);
308 switch (Encoding & 0xF0) {
309 default:
310 llvm_report_error("Do not support this DWARF encoding yet!");
311 break;
312 case dwarf::DW_EH_PE_absptr:
313 // Do nothing special
314 break;
315 case dwarf::DW_EH_PE_pcrel:
316 // FIXME: PCSymbol
317 const MCExpr *PC = MCSymbolRefExpr::Create(".", getContext());
318 Res = MCBinaryExpr::CreateSub(Res, PC, getContext());
319 break;
595 }
596
320 }
321
597 if (Kind.isText()) return TextSection;
598
599 if (Kind.isMergeable1ByteCString() ||
600 Kind.isMergeable2ByteCString() ||
601 Kind.isMergeable4ByteCString()) {
602
603 // We also need alignment here.
604 // FIXME: this is getting the alignment of the character, not the
605 // alignment of the global!
606 unsigned Align =
607 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
608
609 const char *SizeSpec = ".rodata.str1.";
610 if (Kind.isMergeable2ByteCString())
611 SizeSpec = ".rodata.str2.";
612 else if (Kind.isMergeable4ByteCString())
613 SizeSpec = ".rodata.str4.";
614 else
615 assert(Kind.isMergeable1ByteCString() && "unknown string width");
616
617
618 std::string Name = SizeSpec + utostr(Align);
619 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
620 MCSectionELF::SHF_ALLOC |
621 MCSectionELF::SHF_MERGE |
622 MCSectionELF::SHF_STRINGS,
623 Kind);
624 }
625
626 if (Kind.isMergeableConst()) {
627 if (Kind.isMergeableConst4() && MergeableConst4Section)
628 return MergeableConst4Section;
629 if (Kind.isMergeableConst8() && MergeableConst8Section)
630 return MergeableConst8Section;
631 if (Kind.isMergeableConst16() && MergeableConst16Section)
632 return MergeableConst16Section;
633 return ReadOnlySection; // .const
634 }
635
636 if (Kind.isReadOnly()) return ReadOnlySection;
637
638 if (Kind.isThreadData()) return TLSDataSection;
639 if (Kind.isThreadBSS()) return TLSBSSSection;
640
641 // Note: we claim that common symbols are put in BSSSection, but they are
642 // really emitted with the magic .comm directive, which creates a symbol table
643 // entry but not a section.
644 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
645
646 if (Kind.isDataNoRel()) return DataSection;
647 if (Kind.isDataRelLocal()) return DataRelLocalSection;
648 if (Kind.isDataRel()) return DataRelSection;
649 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
650
651 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
652 return DataRelROSection;
322 return Res;
653}
654
323}
324
655/// getSectionForConstant - Given a mergeable constant with the
656/// specified size and relocation information, return a section that it
657/// should be placed in.
658const MCSection *TargetLoweringObjectFileELF::
659getSectionForConstant(SectionKind Kind) const {
660 if (Kind.isMergeableConst4() && MergeableConst4Section)
661 return MergeableConst4Section;
662 if (Kind.isMergeableConst8() && MergeableConst8Section)
663 return MergeableConst8Section;
664 if (Kind.isMergeableConst16() && MergeableConst16Section)
665 return MergeableConst16Section;
666 if (Kind.isReadOnly())
667 return ReadOnlySection;
668
669 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
670 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
671 return DataRelROSection;
325unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
326 return dwarf::DW_EH_PE_absptr;
672}
673
327}
328
674//===----------------------------------------------------------------------===//
675// MachO
676//===----------------------------------------------------------------------===//
677
678typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
679
680TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
681 // If we have the MachO uniquing map, free it.
682 delete (MachOUniqueMapTy*)UniquingMap;
329unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
330 return dwarf::DW_EH_PE_absptr;
683}
684
331}
332
685
686const MCSectionMachO *TargetLoweringObjectFileMachO::
687getMachOSection(StringRef Segment, StringRef Section,
688 unsigned TypeAndAttributes,
689 unsigned Reserved2, SectionKind Kind) const {
690 // We unique sections by their segment/section pair. The returned section
691 // may not have the same flags as the requested section, if so this should be
692 // diagnosed by the client as an error.
693
694 // Create the map if it doesn't already exist.
695 if (UniquingMap == 0)
696 UniquingMap = new MachOUniqueMapTy();
697 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
698
699 // Form the name to look up.
700 SmallString<64> Name;
701 Name += Segment;
702 Name.push_back(',');
703 Name += Section;
704
705 // Do the lookup, if we have a hit, return it.
706 const MCSectionMachO *&Entry = Map[Name.str()];
707 if (Entry) return Entry;
708
709 // Otherwise, return a new section.
710 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
711 Reserved2, Kind, getContext());
333unsigned TargetLoweringObjectFile::getFDEEncoding() const {
334 return dwarf::DW_EH_PE_absptr;
712}
713
335}
336
714
715void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
716 const TargetMachine &TM) {
717 if (UniquingMap != 0)
718 ((MachOUniqueMapTy*)UniquingMap)->clear();
719 TargetLoweringObjectFile::Initialize(Ctx, TM);
720
721 TextSection // .text
722 = getMachOSection("__TEXT", "__text",
723 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
724 SectionKind::getText());
725 DataSection // .data
726 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
727
728 CStringSection // .cstring
729 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
730 SectionKind::getMergeable1ByteCString());
731 UStringSection
732 = getMachOSection("__TEXT","__ustring", 0,
733 SectionKind::getMergeable2ByteCString());
734 FourByteConstantSection // .literal4
735 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
736 SectionKind::getMergeableConst4());
737 EightByteConstantSection // .literal8
738 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
739 SectionKind::getMergeableConst8());
740
741 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
742 // to using it in -static mode.
743 SixteenByteConstantSection = 0;
744 if (TM.getRelocationModel() != Reloc::Static &&
745 TM.getTargetData()->getPointerSize() == 32)
746 SixteenByteConstantSection = // .literal16
747 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
748 SectionKind::getMergeableConst16());
749
750 ReadOnlySection // .const
751 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
752
753 TextCoalSection
754 = getMachOSection("__TEXT", "__textcoal_nt",
755 MCSectionMachO::S_COALESCED |
756 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
757 SectionKind::getText());
758 ConstTextCoalSection
759 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
760 SectionKind::getText());
761 ConstDataCoalSection
762 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
763 SectionKind::getText());
764 ConstDataSection // .const_data
765 = getMachOSection("__DATA", "__const", 0,
766 SectionKind::getReadOnlyWithRel());
767 DataCoalSection
768 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
769 SectionKind::getDataRel());
770 DataCommonSection
771 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
772 SectionKind::getBSS());
773 DataBSSSection
774 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
775 SectionKind::getBSS());
776
777
778 LazySymbolPointerSection
779 = getMachOSection("__DATA", "__la_symbol_ptr",
780 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
781 SectionKind::getMetadata());
782 NonLazySymbolPointerSection
783 = getMachOSection("__DATA", "__nl_symbol_ptr",
784 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
785 SectionKind::getMetadata());
786
787 if (TM.getRelocationModel() == Reloc::Static) {
788 StaticCtorSection
789 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
790 StaticDtorSection
791 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
792 } else {
793 StaticCtorSection
794 = getMachOSection("__DATA", "__mod_init_func",
795 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
796 SectionKind::getDataRel());
797 StaticDtorSection
798 = getMachOSection("__DATA", "__mod_term_func",
799 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
800 SectionKind::getDataRel());
801 }
802
803 // Exception Handling.
804 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
805 SectionKind::getDataRel());
806 EHFrameSection =
807 getMachOSection("__TEXT", "__eh_frame",
808 MCSectionMachO::S_COALESCED |
809 MCSectionMachO::S_ATTR_NO_TOC |
810 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
811 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
812 SectionKind::getReadOnly());
813
814 // Debug Information.
815 DwarfAbbrevSection =
816 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
817 SectionKind::getMetadata());
818 DwarfInfoSection =
819 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
820 SectionKind::getMetadata());
821 DwarfLineSection =
822 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
823 SectionKind::getMetadata());
824 DwarfFrameSection =
825 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
826 SectionKind::getMetadata());
827 DwarfPubNamesSection =
828 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
829 SectionKind::getMetadata());
830 DwarfPubTypesSection =
831 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
832 SectionKind::getMetadata());
833 DwarfStrSection =
834 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
835 SectionKind::getMetadata());
836 DwarfLocSection =
837 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
838 SectionKind::getMetadata());
839 DwarfARangesSection =
840 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
841 SectionKind::getMetadata());
842 DwarfRangesSection =
843 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
844 SectionKind::getMetadata());
845 DwarfMacroInfoSection =
846 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
847 SectionKind::getMetadata());
848 DwarfDebugInlineSection =
849 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
850 SectionKind::getMetadata());
337unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
338 return dwarf::DW_EH_PE_absptr;
851}
852
339}
340
853const MCSection *TargetLoweringObjectFileMachO::
854getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
855 Mangler *Mang, const TargetMachine &TM) const {
856 // Parse the section specifier and create it if valid.
857 StringRef Segment, Section;
858 unsigned TAA, StubSize;
859 std::string ErrorCode =
860 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
861 TAA, StubSize);
862 if (!ErrorCode.empty()) {
863 // If invalid, report the error with llvm_report_error.
864 llvm_report_error("Global variable '" + GV->getNameStr() +
865 "' has an invalid section specifier '" + GV->getSection()+
866 "': " + ErrorCode + ".");
867 // Fall back to dropping it into the data section.
868 return DataSection;
869 }
870
871 // Get the section.
872 const MCSectionMachO *S =
873 getMachOSection(Segment, Section, TAA, StubSize, Kind);
874
875 // Okay, now that we got the section, verify that the TAA & StubSize agree.
876 // If the user declared multiple globals with different section flags, we need
877 // to reject it here.
878 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
879 // If invalid, report the error with llvm_report_error.
880 llvm_report_error("Global variable '" + GV->getNameStr() +
881 "' section type or attributes does not match previous"
882 " section specifier");
883 }
884
885 return S;
886}
887
888const MCSection *TargetLoweringObjectFileMachO::
889SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
890 Mangler *Mang, const TargetMachine &TM) const {
891 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
892
893 if (Kind.isText())
894 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
895
896 // If this is weak/linkonce, put this in a coalescable section, either in text
897 // or data depending on if it is writable.
898 if (GV->isWeakForLinker()) {
899 if (Kind.isReadOnly())
900 return ConstTextCoalSection;
901 return DataCoalSection;
902 }
903
904 // FIXME: Alignment check should be handled by section classifier.
905 if (Kind.isMergeable1ByteCString() ||
906 Kind.isMergeable2ByteCString()) {
907 if (TM.getTargetData()->getPreferredAlignment(
908 cast<GlobalVariable>(GV)) < 32) {
909 if (Kind.isMergeable1ByteCString())
910 return CStringSection;
911 assert(Kind.isMergeable2ByteCString());
912 return UStringSection;
913 }
914 }
915
916 if (Kind.isMergeableConst()) {
917 if (Kind.isMergeableConst4())
918 return FourByteConstantSection;
919 if (Kind.isMergeableConst8())
920 return EightByteConstantSection;
921 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
922 return SixteenByteConstantSection;
923 }
924
925 // Otherwise, if it is readonly, but not something we can specially optimize,
926 // just drop it in .const.
927 if (Kind.isReadOnly())
928 return ReadOnlySection;
929
930 // If this is marked const, put it into a const section. But if the dynamic
931 // linker needs to write to it, put it in the data segment.
932 if (Kind.isReadOnlyWithRel())
933 return ConstDataSection;
934
935 // Put zero initialized globals with strong external linkage in the
936 // DATA, __common section with the .zerofill directive.
937 if (Kind.isBSSExtern())
938 return DataCommonSection;
939
940 // Put zero initialized globals with local linkage in __DATA,__bss directive
941 // with the .zerofill directive (aka .lcomm).
942 if (Kind.isBSSLocal())
943 return DataBSSSection;
944
945 // Otherwise, just drop the variable in the normal data section.
946 return DataSection;
947}
948
949const MCSection *
950TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
951 // If this constant requires a relocation, we have to put it in the data
952 // segment, not in the text segment.
953 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
954 return ConstDataSection;
955
956 if (Kind.isMergeableConst4())
957 return FourByteConstantSection;
958 if (Kind.isMergeableConst8())
959 return EightByteConstantSection;
960 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
961 return SixteenByteConstantSection;
962 return ReadOnlySection; // .const
963}
964
965/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
966/// not to emit the UsedDirective for some symbols in llvm.used.
967// FIXME: REMOVE this (rdar://7071300)
968bool TargetLoweringObjectFileMachO::
969shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
970 /// On Darwin, internally linked data beginning with "L" or "l" does not have
971 /// the directive emitted (this occurs in ObjC metadata).
972 if (!GV) return false;
973
974 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
975 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
976 // FIXME: ObjC metadata is currently emitted as internal symbols that have
977 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
978 // this horrible hack can go away.
979 SmallString<64> Name;
980 Mang->getNameWithPrefix(Name, GV, false);
981 if (Name[0] == 'L' || Name[0] == 'l')
982 return false;
983 }
984
985 return true;
986}
987
988const MCExpr *TargetLoweringObjectFileMachO::
989getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
990 MachineModuleInfo *MMI,
991 bool &IsIndirect, bool &IsPCRel) const {
992 // The mach-o version of this method defaults to returning a stub reference.
993 IsIndirect = true;
994 IsPCRel = false;
995
996 SmallString<128> Name;
997 Mang->getNameWithPrefix(Name, GV, true);
998 Name += "$non_lazy_ptr";
999 return MCSymbolRefExpr::Create(Name.str(), getContext());
1000}
1001
1002
1003//===----------------------------------------------------------------------===//
1004// COFF
1005//===----------------------------------------------------------------------===//
1006
1007typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
1008
1009TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
1010 delete (COFFUniqueMapTy*)UniquingMap;
1011}
1012
1013
1014const MCSection *TargetLoweringObjectFileCOFF::
1015getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
1016 // Create the map if it doesn't already exist.
1017 if (UniquingMap == 0)
1018 UniquingMap = new MachOUniqueMapTy();
1019 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
1020
1021 // Do the lookup, if we have a hit, return it.
1022 const MCSectionCOFF *&Entry = Map[Name];
1023 if (Entry) return Entry;
1024
1025 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
1026}
1027
1028void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1029 const TargetMachine &TM) {
1030 if (UniquingMap != 0)
1031 ((COFFUniqueMapTy*)UniquingMap)->clear();
1032 TargetLoweringObjectFile::Initialize(Ctx, TM);
1033 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1034 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
1035 StaticCtorSection =
1036 getCOFFSection(".ctors", false, SectionKind::getDataRel());
1037 StaticDtorSection =
1038 getCOFFSection(".dtors", false, SectionKind::getDataRel());
1039
1040 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1041 // though it contains relocatable pointers. In PIC mode, this is probably a
1042 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1043 // adjusted or this should be a data section.
1044 LSDASection =
1045 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1046 EHFrameSection =
1047 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
1048
1049 // Debug info.
1050 // FIXME: Don't use 'directive' mode here.
1051 DwarfAbbrevSection =
1052 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1053 true, SectionKind::getMetadata());
1054 DwarfInfoSection =
1055 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1056 true, SectionKind::getMetadata());
1057 DwarfLineSection =
1058 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1059 true, SectionKind::getMetadata());
1060 DwarfFrameSection =
1061 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1062 true, SectionKind::getMetadata());
1063 DwarfPubNamesSection =
1064 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1065 true, SectionKind::getMetadata());
1066 DwarfPubTypesSection =
1067 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1068 true, SectionKind::getMetadata());
1069 DwarfStrSection =
1070 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1071 true, SectionKind::getMetadata());
1072 DwarfLocSection =
1073 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1074 true, SectionKind::getMetadata());
1075 DwarfARangesSection =
1076 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1077 true, SectionKind::getMetadata());
1078 DwarfRangesSection =
1079 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1080 true, SectionKind::getMetadata());
1081 DwarfMacroInfoSection =
1082 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1083 true, SectionKind::getMetadata());
1084}
1085
1086const MCSection *TargetLoweringObjectFileCOFF::
1087getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
1088 Mangler *Mang, const TargetMachine &TM) const {
1089 return getCOFFSection(GV->getSection(), false, Kind);
1090}
1091
1092static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1093 if (Kind.isText())
1094 return ".text$linkonce";
1095 if (Kind.isWriteable())
1096 return ".data$linkonce";
1097 return ".rdata$linkonce";
1098}
1099
1100
1101const MCSection *TargetLoweringObjectFileCOFF::
1102SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1103 Mangler *Mang, const TargetMachine &TM) const {
1104 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1105
1106 // If this global is linkonce/weak and the target handles this by emitting it
1107 // into a 'uniqued' section name, create and return the section now.
1108 if (GV->isWeakForLinker()) {
1109 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1110 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
1111 Mang->getNameWithPrefix(Name, GV, false);
1112 return getCOFFSection(Name.str(), false, Kind);
1113 }
1114
1115 if (Kind.isText())
1116 return getTextSection();
1117
1118 return getDataSection();
1119}
1120