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/MCExpr.h" 14 #include "llvm/MC/MCSymbol.h" 15 #include "llvm/Support/ELF.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 using namespace llvm; 19 20 MCSectionELF::~MCSectionELF() {} // anchor. 21 22 // Decides whether a '.section' directive 23 // should be printed before the section name. 24 bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, 25 const MCAsmInfo &MAI) const { 26 27 if (Unique) 28 return false; 29 30 // FIXME: Does .section .bss/.data/.text work everywhere?? 31 if (Name == ".text" || Name == ".data" || 32 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) 33 return true; 34 35 return false; 36 } 37 38 static void printName(raw_ostream &OS, StringRef Name) { 39 if (Name.find_first_not_of("0123456789_." 40 "abcdefghijklmnopqrstuvwxyz" 41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) { 42 OS << Name; 43 return; 44 } 45 OS << '"'; 46 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) { 47 if (*B == '"') // Unquoted " 48 OS << "\\\""; 49 else if (*B != '\\') // Neither " or backslash 50 OS << *B; 51 else if (B + 1 == E) // Trailing backslash 52 OS << "\\\\"; 53 else { 54 OS << B[0] << B[1]; // Quoted character 55 ++B; 56 } 57 } 58 OS << '"'; 59 } 60 61 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, 62 raw_ostream &OS, 63 const MCExpr *Subsection) const { 64 65 if (ShouldOmitSectionDirective(SectionName, MAI)) { 66 OS << '\t' << getSectionName(); 67 if (Subsection) 68 OS << '\t' << *Subsection; 69 OS << '\n'; 70 return; 71 } 72 73 OS << "\t.section\t"; 74 printName(OS, getSectionName()); 75 76 // Handle the weird solaris syntax if desired. 77 if (MAI.usesSunStyleELFSectionSwitchSyntax() && 78 !(Flags & ELF::SHF_MERGE)) { 79 if (Flags & ELF::SHF_ALLOC) 80 OS << ",#alloc"; 81 if (Flags & ELF::SHF_EXECINSTR) 82 OS << ",#execinstr"; 83 if (Flags & ELF::SHF_WRITE) 84 OS << ",#write"; 85 if (Flags & ELF::SHF_EXCLUDE) 86 OS << ",#exclude"; 87 if (Flags & ELF::SHF_TLS) 88 OS << ",#tls"; 89 OS << '\n'; 90 return; 91 } 92 93 OS << ",\""; 94 if (Flags & ELF::SHF_ALLOC) 95 OS << 'a'; 96 if (Flags & ELF::SHF_EXCLUDE) 97 OS << 'e'; 98 if (Flags & ELF::SHF_EXECINSTR) 99 OS << 'x'; 100 if (Flags & ELF::SHF_GROUP) 101 OS << 'G'; 102 if (Flags & ELF::SHF_WRITE) 103 OS << 'w'; 104 if (Flags & ELF::SHF_MERGE) 105 OS << 'M'; 106 if (Flags & ELF::SHF_STRINGS) 107 OS << 'S'; 108 if (Flags & ELF::SHF_TLS) 109 OS << 'T'; 110 111 // If there are target-specific flags, print them. 112 if (Flags & ELF::XCORE_SHF_CP_SECTION) 113 OS << 'c'; 114 if (Flags & ELF::XCORE_SHF_DP_SECTION) 115 OS << 'd'; 116 117 OS << '"'; 118 119 OS << ','; 120 121 // If comment string is '@', e.g. as on ARM - use '%' instead 122 if (MAI.getCommentString()[0] == '@') 123 OS << '%'; 124 else 125 OS << '@'; 126 127 if (Type == ELF::SHT_INIT_ARRAY) 128 OS << "init_array"; 129 else if (Type == ELF::SHT_FINI_ARRAY) 130 OS << "fini_array"; 131 else if (Type == ELF::SHT_PREINIT_ARRAY) 132 OS << "preinit_array"; 133 else if (Type == ELF::SHT_NOBITS) 134 OS << "nobits"; 135 else if (Type == ELF::SHT_NOTE) 136 OS << "note"; 137 else if (Type == ELF::SHT_PROGBITS) 138 OS << "progbits"; 139 140 if (EntrySize) { 141 assert(Flags & ELF::SHF_MERGE); 142 OS << "," << EntrySize; 143 } 144 145 if (Flags & ELF::SHF_GROUP) { 146 OS << ","; 147 printName(OS, Group->getName()); 148 OS << ",comdat"; 149 } 150 151 if (Unique) 152 OS << ",unique"; 153 154 OS << '\n'; 155 156 if (Subsection) 157 OS << "\t.subsection\t" << *Subsection << '\n'; 158 } 159 160 bool MCSectionELF::UseCodeAlign() const { 161 return getFlags() & ELF::SHF_EXECINSTR; 162 } 163 164 bool MCSectionELF::isVirtualSection() const { 165 return getType() == ELF::SHT_NOBITS; 166 } 167