1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===// 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 #include "llvm/MC/MCSectionELF.h" 11 #include "llvm/MC/MCAsmInfo.h" 12 #include "llvm/MC/MCContext.h" 13 #include "llvm/MC/MCSymbol.h" 14 #include "llvm/Support/raw_ostream.h" 15 using namespace llvm; 16 17 MCSectionELF::~MCSectionELF() {} // anchor. 18 19 // ShouldOmitSectionDirective - Decides whether a '.section' directive 20 // should be printed before the section name 21 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, 22 const MCAsmInfo &MAI) const { 23 24 // FIXME: Does .section .bss/.data/.text work everywhere?? 25 if (Name == ".text" || Name == ".data" || 26 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) 27 return true; 28 29 return false; 30 } 31 32 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, 33 raw_ostream &OS) const { 34 35 if (ShouldOmitSectionDirective(SectionName, MAI)) { 36 OS << '\t' << getSectionName() << '\n'; 37 return; 38 } 39 40 OS << "\t.section\t" << getSectionName(); 41 42 // Handle the weird solaris syntax if desired. 43 if (MAI.usesSunStyleELFSectionSwitchSyntax() && 44 !(Flags & MCSectionELF::SHF_MERGE)) { 45 if (Flags & MCSectionELF::SHF_ALLOC) 46 OS << ",#alloc"; 47 if (Flags & MCSectionELF::SHF_EXECINSTR) 48 OS << ",#execinstr"; 49 if (Flags & MCSectionELF::SHF_WRITE) 50 OS << ",#write"; 51 if (Flags & MCSectionELF::SHF_TLS) 52 OS << ",#tls"; 53 OS << '\n'; 54 return; 55 } 56 57 OS << ",\""; 58 if (Flags & MCSectionELF::SHF_ALLOC) 59 OS << 'a'; 60 if (Flags & MCSectionELF::SHF_EXECINSTR) 61 OS << 'x'; 62 if (Flags & MCSectionELF::SHF_WRITE) 63 OS << 'w'; 64 if (Flags & MCSectionELF::SHF_MERGE) 65 OS << 'M'; 66 if (Flags & MCSectionELF::SHF_STRINGS) 67 OS << 'S'; 68 if (Flags & MCSectionELF::SHF_TLS) 69 OS << 'T'; 70 71 // If there are target-specific flags, print them. 72 if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION) 73 OS << 'c'; 74 if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION) 75 OS << 'd'; 76 77 OS << '"'; 78 79 OS << ','; 80 81 // If comment string is '@', e.g. as on ARM - use '%' instead 82 if (MAI.getCommentString()[0] == '@') 83 OS << '%'; 84 else 85 OS << '@'; 86 87 if (Type == MCSectionELF::SHT_INIT_ARRAY) 88 OS << "init_array"; 89 else if (Type == MCSectionELF::SHT_FINI_ARRAY) 90 OS << "fini_array"; 91 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY) 92 OS << "preinit_array"; 93 else if (Type == MCSectionELF::SHT_NOBITS) 94 OS << "nobits"; 95 else if (Type == MCSectionELF::SHT_NOTE) 96 OS << "note"; 97 else if (Type == MCSectionELF::SHT_PROGBITS) 98 OS << "progbits"; 99 100 if (EntrySize) { 101 assert(Flags & MCSectionELF::SHF_MERGE); 102 OS << "," << EntrySize; 103 } 104 105 OS << '\n'; 106 } 107 108 bool MCSectionELF::UseCodeAlign() const { 109 return getFlags() & MCSectionELF::SHF_EXECINSTR; 110 } 111 112 bool MCSectionELF::isVirtualSection() const { 113 return getType() == MCSectionELF::SHT_NOBITS; 114 } 115 116 // HasCommonSymbols - True if this section holds common symbols, this is 117 // indicated on the ELF object file by a symbol with SHN_COMMON section 118 // header index. 119 bool MCSectionELF::HasCommonSymbols() const { 120 121 if (StringRef(SectionName).startswith(".gnu.linkonce.")) 122 return true; 123 124 return false; 125 } 126 127 unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) { 128 if (Kind.isMergeable1ByteCString()) return 1; 129 if (Kind.isMergeable2ByteCString()) return 2; 130 if (Kind.isMergeable4ByteCString()) return 4; 131 if (Kind.isMergeableConst4()) return 4; 132 if (Kind.isMergeableConst8()) return 8; 133 if (Kind.isMergeableConst16()) return 16; 134 return 0; 135 } 136