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();
68 OS << '\t' << *Subsection;
74 printName(OS, getSectionName());
76 // Handle the weird solaris syntax if desired.
77 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
78 !(Flags & ELF::SHF_MERGE)) {
79 if (Flags & ELF::SHF_ALLOC)
81 if (Flags & ELF::SHF_EXECINSTR)
83 if (Flags & ELF::SHF_WRITE)
85 if (Flags & ELF::SHF_EXCLUDE)
87 if (Flags & ELF::SHF_TLS)
94 if (Flags & ELF::SHF_ALLOC)
96 if (Flags & ELF::SHF_EXCLUDE)
98 if (Flags & ELF::SHF_EXECINSTR)
100 if (Flags & ELF::SHF_GROUP)
102 if (Flags & ELF::SHF_WRITE)
104 if (Flags & ELF::SHF_MERGE)
106 if (Flags & ELF::SHF_STRINGS)
108 if (Flags & ELF::SHF_TLS)
111 // If there are target-specific flags, print them.
112 if (Flags & ELF::XCORE_SHF_CP_SECTION)
114 if (Flags & ELF::XCORE_SHF_DP_SECTION)
121 // If comment string is '@', e.g. as on ARM - use '%' instead
122 if (MAI.getCommentString()[0] == '@')
127 if (Type == ELF::SHT_INIT_ARRAY)
129 else if (Type == ELF::SHT_FINI_ARRAY)
131 else if (Type == ELF::SHT_PREINIT_ARRAY)
132 OS << "preinit_array";
133 else if (Type == ELF::SHT_NOBITS)
135 else if (Type == ELF::SHT_NOTE)
137 else if (Type == ELF::SHT_PROGBITS)
141 assert(Flags & ELF::SHF_MERGE);
142 OS << "," << EntrySize;
145 if (Flags & ELF::SHF_GROUP) {
147 printName(OS, Group->getName());
157 OS << "\t.subsection\t" << *Subsection << '\n';
160 bool MCSectionELF::UseCodeAlign() const {
161 return getFlags() & ELF::SHF_EXECINSTR;
164 bool MCSectionELF::isVirtualSection() const {
165 return getType() == ELF::SHT_NOBITS;