LLVM 22.0.0git
MCAsmInfoELF.cpp
Go to the documentation of this file.
1//===- MCAsmInfoELF.cpp - ELF asm properties ------------------------------===//
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// This file defines target asm properties related what form asm statements
10// should take in general on ELF-based targets
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/Twine.h"
17#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCExpr.h"
24#include <cassert>
25
26using namespace llvm;
27
28void MCAsmInfoELF::anchor() {}
29
30MCSection *MCAsmInfoELF::getNonexecutableStackSection(MCContext &Ctx) const {
31 // Solaris doesn't know/doesn't care about .note.GNU-stack sections, so
32 // don't emit them.
33 if (Ctx.getTargetTriple().isOSSolaris())
34 return nullptr;
35 return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0);
36}
37
38bool MCAsmInfoELF::useCodeAlign(const MCSection &Sec) const {
39 return static_cast<const MCSectionELF &>(Sec).getFlags() & ELF::SHF_EXECINSTR;
40}
41
48
49static void printName(raw_ostream &OS, StringRef Name) {
50 if (Name.find_first_not_of("0123456789_."
51 "abcdefghijklmnopqrstuvwxyz"
52 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
53 OS << Name;
54 return;
55 }
56 OS << '"';
57 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
58 if (*B == '"') // Unquoted "
59 OS << "\\\"";
60 else if (*B != '\\') // Neither " or backslash
61 OS << *B;
62 else if (B + 1 == E) // Trailing backslash
63 OS << "\\\\";
64 else {
65 OS << B[0] << B[1]; // Quoted character
66 ++B;
67 }
68 }
69 OS << '"';
70}
71
72void MCAsmInfoELF::printSwitchToSection(const MCSection &Section,
73 uint32_t Subsection, const Triple &T,
74 raw_ostream &OS) const {
75 auto &Sec = static_cast<const MCSectionELF &>(Section);
76 if (!Sec.isUnique() && shouldOmitSectionDirective(Sec.getName())) {
77 OS << '\t' << Sec.getName();
78 if (Subsection)
79 OS << '\t' << Subsection;
80 OS << '\n';
81 return;
82 }
83
84 OS << "\t.section\t";
85 printName(OS, Sec.getName());
86
87 // Handle the weird solaris syntax if desired.
88 if (usesSunStyleELFSectionSwitchSyntax() && !(Sec.Flags & ELF::SHF_MERGE)) {
89 if (Sec.Flags & ELF::SHF_ALLOC)
90 OS << ",#alloc";
91 if (Sec.Flags & ELF::SHF_EXECINSTR)
92 OS << ",#execinstr";
93 if (Sec.Flags & ELF::SHF_WRITE)
94 OS << ",#write";
95 if (Sec.Flags & ELF::SHF_EXCLUDE)
96 OS << ",#exclude";
97 if (Sec.Flags & ELF::SHF_TLS)
98 OS << ",#tls";
99 OS << '\n';
100 return;
101 }
102
103 OS << ",\"";
104 if (Sec.Flags & ELF::SHF_ALLOC)
105 OS << 'a';
106 if (Sec.Flags & ELF::SHF_EXCLUDE)
107 OS << 'e';
108 if (Sec.Flags & ELF::SHF_EXECINSTR)
109 OS << 'x';
110 if (Sec.Flags & ELF::SHF_WRITE)
111 OS << 'w';
112 if (Sec.Flags & ELF::SHF_MERGE)
113 OS << 'M';
114 if (Sec.Flags & ELF::SHF_STRINGS)
115 OS << 'S';
116 if (Sec.Flags & ELF::SHF_TLS)
117 OS << 'T';
118 if (Sec.Flags & ELF::SHF_LINK_ORDER)
119 OS << 'o';
120 if (Sec.Flags & ELF::SHF_GROUP)
121 OS << 'G';
122 if (Sec.Flags & ELF::SHF_GNU_RETAIN)
123 OS << 'R';
124
125 // If there are os-specific flags, print them.
126 if (T.isOSSolaris())
127 if (Sec.Flags & ELF::SHF_SUNW_NODISCARD)
128 OS << 'R';
129
130 // If there are tarSec.get-specific flags, print them.
131 Triple::ArchType Arch = T.getArch();
132 if (Arch == Triple::xcore) {
133 if (Sec.Flags & ELF::XCORE_SHF_CP_SECTION)
134 OS << 'c';
135 if (Sec.Flags & ELF::XCORE_SHF_DP_SECTION)
136 OS << 'd';
137 } else if (T.isARM() || T.isThumb()) {
138 if (Sec.Flags & ELF::SHF_ARM_PURECODE)
139 OS << 'y';
140 } else if (T.isAArch64()) {
141 if (Sec.Flags & ELF::SHF_AARCH64_PURECODE)
142 OS << 'y';
143 } else if (Arch == Triple::hexagon) {
144 if (Sec.Flags & ELF::SHF_HEX_GPREL)
145 OS << 's';
146 } else if (Arch == Triple::x86_64) {
147 if (Sec.Flags & ELF::SHF_X86_64_LARGE)
148 OS << 'l';
149 }
150
151 OS << '"';
152
153 OS << ',';
154
155 // If comment string is '@', e.g. as on ARM - use '%' instead
156 if (getCommentString()[0] == '@')
157 OS << '%';
158 else
159 OS << '@';
160
161 if (Sec.Type == ELF::SHT_INIT_ARRAY)
162 OS << "init_array";
163 else if (Sec.Type == ELF::SHT_FINI_ARRAY)
164 OS << "fini_array";
165 else if (Sec.Type == ELF::SHT_PREINIT_ARRAY)
166 OS << "preinit_array";
167 else if (Sec.Type == ELF::SHT_NOBITS)
168 OS << "nobits";
169 else if (Sec.Type == ELF::SHT_NOTE)
170 OS << "note";
171 else if (Sec.Type == ELF::SHT_PROGBITS)
172 OS << "progbits";
173 else if (Sec.Type == ELF::SHT_X86_64_UNWIND)
174 OS << "unwind";
175 else if (Sec.Type == ELF::SHT_MIPS_DWARF)
176 // Print hex value of the flag while we do not have
177 // any standard symbolic representation of the flag.
178 OS << "0x7000001e";
179 else if (Sec.Type == ELF::SHT_LLVM_ODRTAB)
180 OS << "llvm_odrtab";
181 else if (Sec.Type == ELF::SHT_LLVM_LINKER_OPTIONS)
182 OS << "llvm_linker_options";
183 else if (Sec.Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
184 OS << "llvm_call_graph_profile";
185 else if (Sec.Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
186 OS << "llvm_dependent_libraries";
187 else if (Sec.Type == ELF::SHT_LLVM_SYMPART)
188 OS << "llvm_sympart";
189 else if (Sec.Type == ELF::SHT_LLVM_BB_ADDR_MAP)
190 OS << "llvm_bb_addr_map";
191 else if (Sec.Type == ELF::SHT_LLVM_OFFLOADING)
192 OS << "llvm_offloading";
193 else if (Sec.Type == ELF::SHT_LLVM_LTO)
194 OS << "llvm_lto";
195 else if (Sec.Type == ELF::SHT_LLVM_JT_SIZES)
196 OS << "llvm_jt_sizes";
197 else if (Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE)
198 OS << "llvm_cfi_jump_table";
199 else
200 OS << "0x" << Twine::utohexstr(Sec.Type);
201
202 if (Sec.EntrySize) {
203 assert((Sec.Flags & ELF::SHF_MERGE) ||
204 Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE);
205 OS << "," << Sec.EntrySize;
206 }
207
208 if (Sec.Flags & ELF::SHF_LINK_ORDER) {
209 OS << ",";
210 if (Sec.LinkedToSym)
211 printName(OS, Sec.LinkedToSym->getName());
212 else
213 OS << '0';
214 }
215
216 if (Sec.Flags & ELF::SHF_GROUP) {
217 OS << ",";
218 printName(OS, Sec.Group.getPointer()->getName());
219 if (Sec.isComdat())
220 OS << ",comdat";
221 }
222
223 if (Sec.isUnique())
224 OS << ",unique," << Sec.UniqueID;
225
226 OS << '\n';
227
228 if (Subsection) {
229 OS << "\t.subsection\t" << Subsection;
230 OS << '\n';
231 }
232}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void printName(raw_ostream &OS, StringRef Name)
#define T
static uint32_t getFlags(const Symbol *Sym)
Definition TapiFile.cpp:26
bool usesSunStyleELFSectionSwitchSyntax() const
Definition MCAsmInfo.h:505
StringRef PrivateGlobalPrefix
This prefix is used for globals like constant pool entries that are completely private to the ....
Definition MCAsmInfo.h:160
const char * WeakRefDirective
This directive, if non-null, is used to declare a global as being a weak undefined symbol.
Definition MCAsmInfo.h:325
StringRef PrivateLabelPrefix
This prefix is used for labels for basic blocks.
Definition MCAsmInfo.h:164
StringRef getCommentString() const
Definition MCAsmInfo.h:538
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
bool HasIdentDirective
True if the target has a .ident directive, this is true for ELF targets.
Definition MCAsmInfo.h:314
Context object for machine code objects.
Definition MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:553
const Triple & getTargetTriple() const
Definition MCContext.h:400
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:521
StringRef getName() const
Definition MCSection.h:590
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
bool isOSSolaris() const
Definition Triple.h:648
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ SHT_LLVM_JT_SIZES
Definition ELF.h:1184
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition ELF.h:1174
@ SHT_PROGBITS
Definition ELF.h:1143
@ SHT_LLVM_LINKER_OPTIONS
Definition ELF.h:1171
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition ELF.h:1180
@ SHT_NOBITS
Definition ELF.h:1150
@ SHT_LLVM_ODRTAB
Definition ELF.h:1170
@ SHT_LLVM_OFFLOADING
Definition ELF.h:1182
@ SHT_LLVM_LTO
Definition ELF.h:1183
@ SHT_MIPS_DWARF
Definition ELF.h:1221
@ SHT_PREINIT_ARRAY
Definition ELF.h:1156
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1181
@ SHT_LLVM_CFI_JUMP_TABLE
Definition ELF.h:1185
@ SHT_INIT_ARRAY
Definition ELF.h:1154
@ SHT_NOTE
Definition ELF.h:1149
@ SHT_X86_64_UNWIND
Definition ELF.h:1217
@ SHT_LLVM_SYMPART
Definition ELF.h:1176
@ SHT_FINI_ARRAY
Definition ELF.h:1155
@ XCORE_SHF_DP_SECTION
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition ELF.h:1292
@ SHF_MERGE
Definition ELF.h:1249
@ SHF_STRINGS
Definition ELF.h:1252
@ SHF_AARCH64_PURECODE
Definition ELF.h:1341
@ XCORE_SHF_CP_SECTION
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition ELF.h:1297
@ SHF_EXCLUDE
Definition ELF.h:1277
@ SHF_ALLOC
Definition ELF.h:1243
@ SHF_LINK_ORDER
Definition ELF.h:1258
@ SHF_HEX_GPREL
Definition ELF.h:1310
@ SHF_GROUP
Definition ELF.h:1265
@ SHF_SUNW_NODISCARD
Definition ELF.h:1284
@ SHF_X86_64_LARGE
Definition ELF.h:1306
@ SHF_GNU_RETAIN
Definition ELF.h:1274
@ SHF_WRITE
Definition ELF.h:1240
@ SHF_TLS
Definition ELF.h:1268
@ SHF_ARM_PURECODE
Definition ELF.h:1338
@ SHF_EXECINSTR
Definition ELF.h:1246
This is an optimization pass for GlobalISel generic memory operations.