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