LLVM 23.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::getStackSection(MCContext &Ctx, bool Exec) 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,
36 Exec ? ELF::SHF_EXECINSTR : 0U);
37}
38
39bool MCAsmInfoELF::useCodeAlign(const MCSection &Sec) const {
40 return static_cast<const MCSectionELF &>(Sec).getFlags() & ELF::SHF_EXECINSTR;
41}
42
50
51static void printName(raw_ostream &OS, StringRef Name) {
52 if (Name.find_first_not_of("0123456789_."
53 "abcdefghijklmnopqrstuvwxyz"
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
55 OS << Name;
56 return;
57 }
58 OS << '"';
59 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
60 if (*B == '"') // Unquoted "
61 OS << "\\\"";
62 else if (*B != '\\') // Neither " or backslash
63 OS << *B;
64 else if (B + 1 == E) // Trailing backslash
65 OS << "\\\\";
66 else {
67 OS << B[0] << B[1]; // Quoted character
68 ++B;
69 }
70 }
71 OS << '"';
72}
73
74void MCAsmInfoELF::printSwitchToSection(const MCSection &Section,
75 uint32_t Subsection, const Triple &T,
76 raw_ostream &OS) const {
77 auto &Sec = static_cast<const MCSectionELF &>(Section);
78 if (!Sec.isUnique() && shouldOmitSectionDirective(Sec.getName())) {
79 OS << '\t' << Sec.getName();
80 if (Subsection)
81 OS << '\t' << Subsection;
82 OS << '\n';
83 return;
84 }
85
86 OS << "\t.section\t";
87 printName(OS, Sec.getName());
88
89 // Handle the weird solaris syntax if desired.
90 if (usesSunStyleELFSectionSwitchSyntax() && !(Sec.Flags & ELF::SHF_MERGE)) {
91 if (Sec.Flags & ELF::SHF_ALLOC)
92 OS << ",#alloc";
93 if (Sec.Flags & ELF::SHF_EXECINSTR)
94 OS << ",#execinstr";
95 if (Sec.Flags & ELF::SHF_WRITE)
96 OS << ",#write";
97 if (Sec.Flags & ELF::SHF_EXCLUDE)
98 OS << ",#exclude";
99 if (Sec.Flags & ELF::SHF_TLS)
100 OS << ",#tls";
101 OS << '\n';
102 return;
103 }
104
105 OS << ",\"";
106 if (Sec.Flags & ELF::SHF_ALLOC)
107 OS << 'a';
108 if (Sec.Flags & ELF::SHF_EXCLUDE)
109 OS << 'e';
110 if (Sec.Flags & ELF::SHF_EXECINSTR)
111 OS << 'x';
112 if (Sec.Flags & ELF::SHF_WRITE)
113 OS << 'w';
114 if (Sec.Flags & ELF::SHF_MERGE)
115 OS << 'M';
116 if (Sec.Flags & ELF::SHF_STRINGS)
117 OS << 'S';
118 if (Sec.Flags & ELF::SHF_TLS)
119 OS << 'T';
120 if (Sec.Flags & ELF::SHF_LINK_ORDER)
121 OS << 'o';
122 if (Sec.Flags & ELF::SHF_GROUP)
123 OS << 'G';
124 if (Sec.Flags & ELF::SHF_GNU_RETAIN)
125 OS << 'R';
126
127 // If there are os-specific flags, print them.
128 if (T.isOSSolaris())
129 if (Sec.Flags & ELF::SHF_SUNW_NODISCARD)
130 OS << 'R';
131
132 // If there are tarSec.get-specific flags, print them.
133 Triple::ArchType Arch = T.getArch();
134 if (Arch == Triple::xcore) {
135 if (Sec.Flags & ELF::XCORE_SHF_CP_SECTION)
136 OS << 'c';
137 if (Sec.Flags & ELF::XCORE_SHF_DP_SECTION)
138 OS << 'd';
139 } else if (T.isARM() || T.isThumb()) {
140 if (Sec.Flags & ELF::SHF_ARM_PURECODE)
141 OS << 'y';
142 } else if (T.isAArch64()) {
143 if (Sec.Flags & ELF::SHF_AARCH64_PURECODE)
144 OS << 'y';
145 } else if (Arch == Triple::hexagon) {
146 if (Sec.Flags & ELF::SHF_HEX_GPREL)
147 OS << 's';
148 } else if (Arch == Triple::x86_64) {
149 if (Sec.Flags & ELF::SHF_X86_64_LARGE)
150 OS << 'l';
151 }
152
153 OS << '"';
154
155 OS << ',';
156
157 // If comment string is '@', e.g. as on ARM - use '%' instead
158 if (getCommentString()[0] == '@')
159 OS << '%';
160 else
161 OS << '@';
162
163 if (Sec.Type == ELF::SHT_INIT_ARRAY)
164 OS << "init_array";
165 else if (Sec.Type == ELF::SHT_FINI_ARRAY)
166 OS << "fini_array";
167 else if (Sec.Type == ELF::SHT_PREINIT_ARRAY)
168 OS << "preinit_array";
169 else if (Sec.Type == ELF::SHT_NOBITS)
170 OS << "nobits";
171 else if (Sec.Type == ELF::SHT_NOTE)
172 OS << "note";
173 else if (Sec.Type == ELF::SHT_PROGBITS)
174 OS << "progbits";
175 else if (Sec.Type == ELF::SHT_X86_64_UNWIND)
176 OS << "unwind";
177 else if (Sec.Type == ELF::SHT_MIPS_DWARF)
178 // Print hex value of the flag while we do not have
179 // any standard symbolic representation of the flag.
180 OS << "0x7000001e";
181 else if (Sec.Type == ELF::SHT_LLVM_ODRTAB)
182 OS << "llvm_odrtab";
183 else if (Sec.Type == ELF::SHT_LLVM_LINKER_OPTIONS)
184 OS << "llvm_linker_options";
185 else if (Sec.Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
186 OS << "llvm_call_graph_profile";
187 else if (Sec.Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
188 OS << "llvm_dependent_libraries";
189 else if (Sec.Type == ELF::SHT_LLVM_SYMPART)
190 OS << "llvm_sympart";
191 else if (Sec.Type == ELF::SHT_LLVM_BB_ADDR_MAP)
192 OS << "llvm_bb_addr_map";
193 else if (Sec.Type == ELF::SHT_LLVM_OFFLOADING)
194 OS << "llvm_offloading";
195 else if (Sec.Type == ELF::SHT_LLVM_LTO)
196 OS << "llvm_lto";
197 else if (Sec.Type == ELF::SHT_LLVM_JT_SIZES)
198 OS << "llvm_jt_sizes";
199 else if (Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE)
200 OS << "llvm_cfi_jump_table";
201 else if (Sec.Type == ELF::SHT_LLVM_CALL_GRAPH)
202 OS << "llvm_call_graph";
203 else
204 OS << "0x" << Twine::utohexstr(Sec.Type);
205
206 if (Sec.EntrySize) {
207 assert((Sec.Flags & ELF::SHF_MERGE) ||
208 Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE);
209 OS << "," << Sec.EntrySize;
210 }
211
212 if (Sec.Flags & ELF::SHF_LINK_ORDER) {
213 OS << ",";
214 if (Sec.LinkedToSym)
215 printName(OS, Sec.LinkedToSym->getName());
216 else
217 OS << '0';
218 }
219
220 if (Sec.Flags & ELF::SHF_GROUP) {
221 OS << ",";
222 printName(OS, Sec.Group.getPointer()->getName());
223 if (Sec.isComdat())
224 OS << ",comdat";
225 }
226
227 if (Sec.isUnique())
228 OS << ",unique," << Sec.UniqueID;
229
230 OS << '\n';
231
232 if (Subsection) {
233 OS << "\t.subsection\t" << Subsection;
234 OS << '\n';
235 }
236}
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:508
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:328
StringRef PrivateLabelPrefix
This prefix is used for labels for basic blocks.
Definition MCAsmInfo.h:164
StringRef getCommentString() const
Definition MCAsmInfo.h:541
bool HasPreferredAlignment
Definition MCAsmInfo.h:305
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:317
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:517
StringRef getName() const
Definition MCSection.h:587
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:669
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
@ 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:1297
@ SHF_MERGE
Definition ELF.h:1254
@ SHF_STRINGS
Definition ELF.h:1257
@ SHF_AARCH64_PURECODE
Definition ELF.h:1346
@ 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:1302
@ SHF_EXCLUDE
Definition ELF.h:1282
@ SHF_ALLOC
Definition ELF.h:1248
@ SHF_LINK_ORDER
Definition ELF.h:1263
@ SHF_HEX_GPREL
Definition ELF.h:1315
@ SHF_GROUP
Definition ELF.h:1270
@ SHF_SUNW_NODISCARD
Definition ELF.h:1289
@ SHF_X86_64_LARGE
Definition ELF.h:1311
@ SHF_GNU_RETAIN
Definition ELF.h:1279
@ SHF_WRITE
Definition ELF.h:1245
@ SHF_TLS
Definition ELF.h:1273
@ SHF_ARM_PURECODE
Definition ELF.h:1343
@ SHF_EXECINSTR
Definition ELF.h:1251
@ SHT_LLVM_JT_SIZES
Definition ELF.h:1188
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition ELF.h:1178
@ SHT_PROGBITS
Definition ELF.h:1147
@ SHT_LLVM_LINKER_OPTIONS
Definition ELF.h:1175
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition ELF.h:1184
@ SHT_NOBITS
Definition ELF.h:1154
@ SHT_LLVM_ODRTAB
Definition ELF.h:1174
@ SHT_LLVM_OFFLOADING
Definition ELF.h:1186
@ SHT_LLVM_LTO
Definition ELF.h:1187
@ SHT_LLVM_CALL_GRAPH
Definition ELF.h:1190
@ SHT_MIPS_DWARF
Definition ELF.h:1226
@ SHT_PREINIT_ARRAY
Definition ELF.h:1160
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1185
@ SHT_LLVM_CFI_JUMP_TABLE
Definition ELF.h:1189
@ SHT_INIT_ARRAY
Definition ELF.h:1158
@ SHT_NOTE
Definition ELF.h:1153
@ SHT_X86_64_UNWIND
Definition ELF.h:1222
@ SHT_LLVM_SYMPART
Definition ELF.h:1180
@ SHT_FINI_ARRAY
Definition ELF.h:1159
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26