1 //===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCSectionELF.h"
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/Support/ErrorHandling.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   if (Flags & ELF::SHF_LINK_ORDER)
106     OS << 'o';
107 
108   // If there are target-specific flags, print them.
109   Triple::ArchType Arch = T.getArch();
110   if (Arch == Triple::xcore) {
111     if (Flags & ELF::XCORE_SHF_CP_SECTION)
112       OS << 'c';
113     if (Flags & ELF::XCORE_SHF_DP_SECTION)
114       OS << 'd';
115   } else if (T.isARM() || T.isThumb()) {
116     if (Flags & ELF::SHF_ARM_PURECODE)
117       OS << 'y';
118   } else if (Arch == Triple::hexagon) {
119     if (Flags & ELF::SHF_HEX_GPREL)
120       OS << 's';
121   }
122 
123   OS << '"';
124 
125   OS << ',';
126 
127   // If comment string is '@', e.g. as on ARM - use '%' instead
128   if (MAI.getCommentString()[0] == '@')
129     OS << '%';
130   else
131     OS << '@';
132 
133   if (Type == ELF::SHT_INIT_ARRAY)
134     OS << "init_array";
135   else if (Type == ELF::SHT_FINI_ARRAY)
136     OS << "fini_array";
137   else if (Type == ELF::SHT_PREINIT_ARRAY)
138     OS << "preinit_array";
139   else if (Type == ELF::SHT_NOBITS)
140     OS << "nobits";
141   else if (Type == ELF::SHT_NOTE)
142     OS << "note";
143   else if (Type == ELF::SHT_PROGBITS)
144     OS << "progbits";
145   else if (Type == ELF::SHT_X86_64_UNWIND)
146     OS << "unwind";
147   else if (Type == ELF::SHT_MIPS_DWARF)
148     // Print hex value of the flag while we do not have
149     // any standard symbolic representation of the flag.
150     OS << "0x7000001e";
151   else if (Type == ELF::SHT_LLVM_ODRTAB)
152     OS << "llvm_odrtab";
153   else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
154     OS << "llvm_linker_options";
155   else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
156     OS << "llvm_call_graph_profile";
157   else
158     report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
159                        " for section " + getSectionName());
160 
161   if (EntrySize) {
162     assert(Flags & ELF::SHF_MERGE);
163     OS << "," << EntrySize;
164   }
165 
166   if (Flags & ELF::SHF_GROUP) {
167     OS << ",";
168     printName(OS, Group->getName());
169     OS << ",comdat";
170   }
171 
172   if (Flags & ELF::SHF_LINK_ORDER) {
173     assert(AssociatedSymbol);
174     OS << ",";
175     printName(OS, AssociatedSymbol->getName());
176   }
177 
178   if (isUnique())
179     OS << ",unique," << UniqueID;
180 
181   OS << '\n';
182 
183   if (Subsection) {
184     OS << "\t.subsection\t";
185     Subsection->print(OS, &MAI);
186     OS << '\n';
187   }
188 }
189 
190 bool MCSectionELF::UseCodeAlign() const {
191   return getFlags() & ELF::SHF_EXECINSTR;
192 }
193 
194 bool MCSectionELF::isVirtualSection() const {
195   return getType() == ELF::SHT_NOBITS;
196 }
197