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 return MAI.shouldOmitSectionDirective(Name);
33 static void printName(raw_ostream &OS, StringRef Name) {
34 if (Name.find_first_not_of("0123456789_."
35 "abcdefghijklmnopqrstuvwxyz"
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
41 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
42 if (*B == '"') // Unquoted "
44 else if (*B != '\\') // Neither " or backslash
46 else if (B + 1 == E) // Trailing backslash
49 OS << B[0] << B[1]; // Quoted character
56 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
58 const MCExpr *Subsection) const {
60 if (ShouldOmitSectionDirective(SectionName, MAI)) {
61 OS << '\t' << getSectionName();
64 Subsection->print(OS, &MAI);
71 printName(OS, getSectionName());
73 // Handle the weird solaris syntax if desired.
74 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
75 !(Flags & ELF::SHF_MERGE)) {
76 if (Flags & ELF::SHF_ALLOC)
78 if (Flags & ELF::SHF_EXECINSTR)
80 if (Flags & ELF::SHF_WRITE)
82 if (Flags & ELF::SHF_EXCLUDE)
84 if (Flags & ELF::SHF_TLS)
91 if (Flags & ELF::SHF_ALLOC)
93 if (Flags & ELF::SHF_EXCLUDE)
95 if (Flags & ELF::SHF_EXECINSTR)
97 if (Flags & ELF::SHF_GROUP)
99 if (Flags & ELF::SHF_WRITE)
101 if (Flags & ELF::SHF_MERGE)
103 if (Flags & ELF::SHF_STRINGS)
105 if (Flags & ELF::SHF_TLS)
108 // If there are target-specific flags, print them.
109 if (Flags & ELF::XCORE_SHF_CP_SECTION)
111 if (Flags & ELF::XCORE_SHF_DP_SECTION)
118 // If comment string is '@', e.g. as on ARM - use '%' instead
119 if (MAI.getCommentString()[0] == '@')
124 if (Type == ELF::SHT_INIT_ARRAY)
126 else if (Type == ELF::SHT_FINI_ARRAY)
128 else if (Type == ELF::SHT_PREINIT_ARRAY)
129 OS << "preinit_array";
130 else if (Type == ELF::SHT_NOBITS)
132 else if (Type == ELF::SHT_NOTE)
134 else if (Type == ELF::SHT_PROGBITS)
136 else if (Type == ELF::SHT_X86_64_UNWIND)
140 assert(Flags & ELF::SHF_MERGE);
141 OS << "," << EntrySize;
144 if (Flags & ELF::SHF_GROUP) {
146 printName(OS, Group->getName());
151 OS << ",unique," << UniqueID;
156 OS << "\t.subsection\t";
157 Subsection->print(OS, &MAI);
162 bool MCSectionELF::UseCodeAlign() const {
163 return getFlags() & ELF::SHF_EXECINSTR;
166 bool MCSectionELF::isVirtualSection() const {
167 return getType() == ELF::SHT_NOBITS;