1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
20 MCSectionELF::~MCSectionELF() {} // anchor.
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 {
30 // FIXME: Does .section .bss/.data/.text work everywhere??
31 if (Name == ".text" || Name == ".data" ||
32 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
38 static void printName(raw_ostream &OS, StringRef Name) {
39 if (Name.find_first_not_of("0123456789_."
40 "abcdefghijklmnopqrstuvwxyz"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
46 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
47 if (*B == '"') // Unquoted "
49 else if (*B != '\\') // Neither " or backslash
51 else if (B + 1 == E) // Trailing backslash
54 OS << B[0] << B[1]; // Quoted character
61 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
63 const MCExpr *Subsection) const {
65 if (ShouldOmitSectionDirective(SectionName, MAI)) {
66 OS << '\t' << getSectionName();
69 Subsection->print(OS, &MAI);
76 printName(OS, getSectionName());
78 // Handle the weird solaris syntax if desired.
79 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
80 !(Flags & ELF::SHF_MERGE)) {
81 if (Flags & ELF::SHF_ALLOC)
83 if (Flags & ELF::SHF_EXECINSTR)
85 if (Flags & ELF::SHF_WRITE)
87 if (Flags & ELF::SHF_EXCLUDE)
89 if (Flags & ELF::SHF_TLS)
96 if (Flags & ELF::SHF_ALLOC)
98 if (Flags & ELF::SHF_EXCLUDE)
100 if (Flags & ELF::SHF_EXECINSTR)
102 if (Flags & ELF::SHF_GROUP)
104 if (Flags & ELF::SHF_WRITE)
106 if (Flags & ELF::SHF_MERGE)
108 if (Flags & ELF::SHF_STRINGS)
110 if (Flags & ELF::SHF_TLS)
113 // If there are target-specific flags, print them.
114 if (Flags & ELF::XCORE_SHF_CP_SECTION)
116 if (Flags & ELF::XCORE_SHF_DP_SECTION)
123 // If comment string is '@', e.g. as on ARM - use '%' instead
124 if (MAI.getCommentString()[0] == '@')
129 if (Type == ELF::SHT_INIT_ARRAY)
131 else if (Type == ELF::SHT_FINI_ARRAY)
133 else if (Type == ELF::SHT_PREINIT_ARRAY)
134 OS << "preinit_array";
135 else if (Type == ELF::SHT_NOBITS)
137 else if (Type == ELF::SHT_NOTE)
139 else if (Type == ELF::SHT_PROGBITS)
143 assert(Flags & ELF::SHF_MERGE);
144 OS << "," << EntrySize;
147 if (Flags & ELF::SHF_GROUP) {
149 printName(OS, Group->getName());
154 OS << ",unique," << UniqueID;
159 OS << "\t.subsection\t";
160 Subsection->print(OS, &MAI);
165 bool MCSectionELF::UseCodeAlign() const {
166 return getFlags() & ELF::SHF_EXECINSTR;
169 bool MCSectionELF::isVirtualSection() const {
170 return getType() == ELF::SHT_NOBITS;