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