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/Twine.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 "llvm/TargetParser/Triple.h" |
17 | #include <cassert> |
18 | |
19 | using namespace llvm; |
20 | |
21 | // Decides whether a '.section' directive |
22 | // should be printed before the section name. |
23 | bool MCSectionELF::shouldOmitSectionDirective(StringRef Name, |
24 | const MCAsmInfo &MAI) const { |
25 | if (isUnique()) |
26 | return false; |
27 | |
28 | return MAI.shouldOmitSectionDirective(SectionName: Name); |
29 | } |
30 | |
31 | static void printName(raw_ostream &OS, StringRef Name) { |
32 | if (Name.find_first_not_of(Chars: "0123456789_." |
33 | "abcdefghijklmnopqrstuvwxyz" |
34 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) == Name.npos) { |
35 | OS << Name; |
36 | return; |
37 | } |
38 | OS << '"'; |
39 | for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) { |
40 | if (*B == '"') // Unquoted " |
41 | OS << "\\\"" ; |
42 | else if (*B != '\\') // Neither " or backslash |
43 | OS << *B; |
44 | else if (B + 1 == E) // Trailing backslash |
45 | OS << "\\\\" ; |
46 | else { |
47 | OS << B[0] << B[1]; // Quoted character |
48 | ++B; |
49 | } |
50 | } |
51 | OS << '"'; |
52 | } |
53 | |
54 | void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T, |
55 | raw_ostream &OS, |
56 | uint32_t Subsection) const { |
57 | if (shouldOmitSectionDirective(Name: getName(), MAI)) { |
58 | OS << '\t' << getName(); |
59 | if (Subsection) |
60 | OS << '\t' << Subsection; |
61 | OS << '\n'; |
62 | return; |
63 | } |
64 | |
65 | OS << "\t.section\t" ; |
66 | printName(OS, Name: getName()); |
67 | |
68 | // Handle the weird solaris syntax if desired. |
69 | if (MAI.usesSunStyleELFSectionSwitchSyntax() && |
70 | !(Flags & ELF::SHF_MERGE)) { |
71 | if (Flags & ELF::SHF_ALLOC) |
72 | OS << ",#alloc" ; |
73 | if (Flags & ELF::SHF_EXECINSTR) |
74 | OS << ",#execinstr" ; |
75 | if (Flags & ELF::SHF_WRITE) |
76 | OS << ",#write" ; |
77 | if (Flags & ELF::SHF_EXCLUDE) |
78 | OS << ",#exclude" ; |
79 | if (Flags & ELF::SHF_TLS) |
80 | OS << ",#tls" ; |
81 | OS << '\n'; |
82 | return; |
83 | } |
84 | |
85 | OS << ",\"" ; |
86 | if (Flags & ELF::SHF_ALLOC) |
87 | OS << 'a'; |
88 | if (Flags & ELF::SHF_EXCLUDE) |
89 | OS << 'e'; |
90 | if (Flags & ELF::SHF_EXECINSTR) |
91 | OS << 'x'; |
92 | if (Flags & ELF::SHF_WRITE) |
93 | OS << 'w'; |
94 | if (Flags & ELF::SHF_MERGE) |
95 | OS << 'M'; |
96 | if (Flags & ELF::SHF_STRINGS) |
97 | OS << 'S'; |
98 | if (Flags & ELF::SHF_TLS) |
99 | OS << 'T'; |
100 | if (Flags & ELF::SHF_LINK_ORDER) |
101 | OS << 'o'; |
102 | if (Flags & ELF::SHF_GROUP) |
103 | OS << 'G'; |
104 | if (Flags & ELF::SHF_GNU_RETAIN) |
105 | OS << 'R'; |
106 | |
107 | // If there are os-specific flags, print them. |
108 | if (T.isOSSolaris()) |
109 | if (Flags & ELF::SHF_SUNW_NODISCARD) |
110 | OS << 'R'; |
111 | |
112 | // If there are target-specific flags, print them. |
113 | Triple::ArchType Arch = T.getArch(); |
114 | if (Arch == Triple::xcore) { |
115 | if (Flags & ELF::XCORE_SHF_CP_SECTION) |
116 | OS << 'c'; |
117 | if (Flags & ELF::XCORE_SHF_DP_SECTION) |
118 | OS << 'd'; |
119 | } else if (T.isARM() || T.isThumb()) { |
120 | if (Flags & ELF::SHF_ARM_PURECODE) |
121 | OS << 'y'; |
122 | } else if (T.isAArch64()) { |
123 | if (Flags & ELF::SHF_AARCH64_PURECODE) |
124 | OS << 'y'; |
125 | } else if (Arch == Triple::hexagon) { |
126 | if (Flags & ELF::SHF_HEX_GPREL) |
127 | OS << 's'; |
128 | } else if (Arch == Triple::x86_64) { |
129 | if (Flags & ELF::SHF_X86_64_LARGE) |
130 | OS << 'l'; |
131 | } |
132 | |
133 | OS << '"'; |
134 | |
135 | OS << ','; |
136 | |
137 | // If comment string is '@', e.g. as on ARM - use '%' instead |
138 | if (MAI.getCommentString()[0] == '@') |
139 | OS << '%'; |
140 | else |
141 | OS << '@'; |
142 | |
143 | if (Type == ELF::SHT_INIT_ARRAY) |
144 | OS << "init_array" ; |
145 | else if (Type == ELF::SHT_FINI_ARRAY) |
146 | OS << "fini_array" ; |
147 | else if (Type == ELF::SHT_PREINIT_ARRAY) |
148 | OS << "preinit_array" ; |
149 | else if (Type == ELF::SHT_NOBITS) |
150 | OS << "nobits" ; |
151 | else if (Type == ELF::SHT_NOTE) |
152 | OS << "note" ; |
153 | else if (Type == ELF::SHT_PROGBITS) |
154 | OS << "progbits" ; |
155 | else if (Type == ELF::SHT_X86_64_UNWIND) |
156 | OS << "unwind" ; |
157 | else if (Type == ELF::SHT_MIPS_DWARF) |
158 | // Print hex value of the flag while we do not have |
159 | // any standard symbolic representation of the flag. |
160 | OS << "0x7000001e" ; |
161 | else if (Type == ELF::SHT_LLVM_ODRTAB) |
162 | OS << "llvm_odrtab" ; |
163 | else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS) |
164 | OS << "llvm_linker_options" ; |
165 | else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) |
166 | OS << "llvm_call_graph_profile" ; |
167 | else if (Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES) |
168 | OS << "llvm_dependent_libraries" ; |
169 | else if (Type == ELF::SHT_LLVM_SYMPART) |
170 | OS << "llvm_sympart" ; |
171 | else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP) |
172 | OS << "llvm_bb_addr_map" ; |
173 | else if (Type == ELF::SHT_LLVM_OFFLOADING) |
174 | OS << "llvm_offloading" ; |
175 | else if (Type == ELF::SHT_LLVM_LTO) |
176 | OS << "llvm_lto" ; |
177 | else if (Type == ELF::SHT_LLVM_JT_SIZES) |
178 | OS << "llvm_jt_sizes" ; |
179 | else |
180 | OS << "0x" << Twine::utohexstr(Val: Type); |
181 | |
182 | if (EntrySize) { |
183 | assert(Flags & ELF::SHF_MERGE); |
184 | OS << "," << EntrySize; |
185 | } |
186 | |
187 | if (Flags & ELF::SHF_LINK_ORDER) { |
188 | OS << "," ; |
189 | if (LinkedToSym) |
190 | printName(OS, Name: LinkedToSym->getName()); |
191 | else |
192 | OS << '0'; |
193 | } |
194 | |
195 | if (Flags & ELF::SHF_GROUP) { |
196 | OS << "," ; |
197 | printName(OS, Name: Group.getPointer()->getName()); |
198 | if (isComdat()) |
199 | OS << ",comdat" ; |
200 | } |
201 | |
202 | if (isUnique()) |
203 | OS << ",unique," << UniqueID; |
204 | |
205 | OS << '\n'; |
206 | |
207 | if (Subsection) { |
208 | OS << "\t.subsection\t" << Subsection; |
209 | OS << '\n'; |
210 | } |
211 | } |
212 | |
213 | bool MCSectionELF::useCodeAlign() const { |
214 | return getFlags() & ELF::SHF_EXECINSTR; |
215 | } |
216 | |
217 | StringRef MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS" ; } |
218 | |