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
51
52static void printName(raw_ostream &OS, StringRef Name) {
53 if (Name.find_first_not_of("0123456789_."
54 "abcdefghijklmnopqrstuvwxyz"
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
56 OS << Name;
57 return;
58 }
59 OS << '"';
60 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
61 if (*B == '"') // Unquoted "
62 OS << "\\\"";
63 else if (*B != '\\') // Neither " or backslash
64 OS << *B;
65 else if (B + 1 == E) // Trailing backslash
66 OS << "\\\\";
67 else {
68 OS << B[0] << B[1]; // Quoted character
69 ++B;
70 }
71 }
72 OS << '"';
73}
74
75void MCAsmInfoELF::printSwitchToSection(const MCSection &Section,
76 uint32_t Subsection, const Triple &T,
77 raw_ostream &OS) const {
78 auto &Sec = static_cast<const MCSectionELF &>(Section);
79 if (!Sec.isUnique() && shouldOmitSectionDirective(Sec.getName())) {
80 OS << '\t' << Sec.getName();
81 if (Subsection)
82 OS << '\t' << Subsection;
83 OS << '\n';
84 return;
85 }
86
87 OS << "\t.section\t";
88 printName(OS, Sec.getName());
89
90 // Handle the weird solaris syntax if desired.
91 if (usesSunStyleELFSectionSwitchSyntax() && !(Sec.Flags & ELF::SHF_MERGE)) {
92 if (Sec.Flags & ELF::SHF_ALLOC)
93 OS << ",#alloc";
94 if (Sec.Flags & ELF::SHF_EXECINSTR)
95 OS << ",#execinstr";
96 if (Sec.Flags & ELF::SHF_WRITE)
97 OS << ",#write";
98 if (Sec.Flags & ELF::SHF_EXCLUDE)
99 OS << ",#exclude";
100 if (Sec.Flags & ELF::SHF_TLS)
101 OS << ",#tls";
102 OS << '\n';
103 return;
104 }
105
106 OS << ",\"";
107 if (Sec.Flags & ELF::SHF_ALLOC)
108 OS << 'a';
109 if (Sec.Flags & ELF::SHF_EXCLUDE)
110 OS << 'e';
111 if (Sec.Flags & ELF::SHF_EXECINSTR)
112 OS << 'x';
113 if (Sec.Flags & ELF::SHF_WRITE)
114 OS << 'w';
115 if (Sec.Flags & ELF::SHF_MERGE)
116 OS << 'M';
117 if (Sec.Flags & ELF::SHF_STRINGS)
118 OS << 'S';
119 if (Sec.Flags & ELF::SHF_TLS)
120 OS << 'T';
121 if (Sec.Flags & ELF::SHF_LINK_ORDER)
122 OS << 'o';
123 if (Sec.Flags & ELF::SHF_GROUP)
124 OS << 'G';
125 if (Sec.Flags & ELF::SHF_GNU_RETAIN)
126 OS << 'R';
127
128 // If there are os-specific flags, print them.
129 if (T.isOSSolaris())
130 if (Sec.Flags & ELF::SHF_SUNW_NODISCARD)
131 OS << 'R';
132
133 // If there are tarSec.get-specific flags, print them.
134 Triple::ArchType Arch = T.getArch();
135 if (Arch == Triple::xcore) {
136 if (Sec.Flags & ELF::XCORE_SHF_CP_SECTION)
137 OS << 'c';
138 if (Sec.Flags & ELF::XCORE_SHF_DP_SECTION)
139 OS << 'd';
140 } else if (T.isARM() || T.isThumb()) {
141 if (Sec.Flags & ELF::SHF_ARM_PURECODE)
142 OS << 'y';
143 } else if (T.isAArch64()) {
144 if (Sec.Flags & ELF::SHF_AARCH64_PURECODE)
145 OS << 'y';
146 } else if (Arch == Triple::hexagon) {
147 if (Sec.Flags & ELF::SHF_HEX_GPREL)
148 OS << 's';
149 } else if (Arch == Triple::x86_64) {
150 if (Sec.Flags & ELF::SHF_X86_64_LARGE)
151 OS << 'l';
152 }
153
154 OS << '"';
155
156 OS << ',';
157
158 // If comment string is '@', e.g. as on ARM - use '%' instead
159 if (getCommentString()[0] == '@')
160 OS << '%';
161 else
162 OS << '@';
163
164 if (Sec.Type == ELF::SHT_INIT_ARRAY)
165 OS << "init_array";
166 else if (Sec.Type == ELF::SHT_FINI_ARRAY)
167 OS << "fini_array";
168 else if (Sec.Type == ELF::SHT_PREINIT_ARRAY)
169 OS << "preinit_array";
170 else if (Sec.Type == ELF::SHT_NOBITS)
171 OS << "nobits";
172 else if (Sec.Type == ELF::SHT_NOTE)
173 OS << "note";
174 else if (Sec.Type == ELF::SHT_PROGBITS)
175 OS << "progbits";
176 else if (Sec.Type == ELF::SHT_X86_64_UNWIND)
177 OS << "unwind";
178 else if (Sec.Type == ELF::SHT_MIPS_DWARF)
179 // Print hex value of the flag while we do not have
180 // any standard symbolic representation of the flag.
181 OS << "0x7000001e";
182 else if (Sec.Type == ELF::SHT_LLVM_ODRTAB)
183 OS << "llvm_odrtab";
184 else if (Sec.Type == ELF::SHT_LLVM_LINKER_OPTIONS)
185 OS << "llvm_linker_options";
186 else if (Sec.Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
187 OS << "llvm_call_graph_profile";
188 else if (Sec.Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
189 OS << "llvm_dependent_libraries";
190 else if (Sec.Type == ELF::SHT_LLVM_SYMPART)
191 OS << "llvm_sympart";
192 else if (Sec.Type == ELF::SHT_LLVM_BB_ADDR_MAP)
193 OS << "llvm_bb_addr_map";
194 else if (Sec.Type == ELF::SHT_LLVM_OFFLOADING)
195 OS << "llvm_offloading";
196 else if (Sec.Type == ELF::SHT_LLVM_LTO)
197 OS << "llvm_lto";
198 else if (Sec.Type == ELF::SHT_LLVM_JT_SIZES)
199 OS << "llvm_jt_sizes";
200 else if (Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE)
201 OS << "llvm_cfi_jump_table";
202 else if (Sec.Type == ELF::SHT_LLVM_CALL_GRAPH)
203 OS << "llvm_call_graph";
204 else
205 OS << "0x" << Twine::utohexstr(Sec.Type);
206
207 if (Sec.EntrySize) {
208 assert((Sec.Flags & ELF::SHF_MERGE) ||
209 Sec.Type == ELF::SHT_LLVM_CFI_JUMP_TABLE);
210 OS << "," << Sec.EntrySize;
211 }
212
213 if (Sec.Flags & ELF::SHF_LINK_ORDER) {
214 OS << ",";
215 if (Sec.LinkedToSym)
216 printName(OS, Sec.LinkedToSym->getName());
217 else
218 OS << '0';
219 }
220
221 if (Sec.Flags & ELF::SHF_GROUP) {
222 OS << ",";
223 printName(OS, Sec.Group.getPointer()->getName());
224 if (Sec.isComdat())
225 OS << ",comdat";
226 }
227
228 if (Sec.isUnique())
229 OS << ",unique," << Sec.UniqueID;
230
231 OS << '\n';
232
233 if (Subsection) {
234 OS << "\t.subsection\t" << Subsection;
235 OS << '\n';
236 }
237}
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 LVOptions Options
Definition LVOptions.cpp:25
static void printName(raw_ostream &OS, StringRef Name)
#define T
static uint32_t getFlags(const Symbol *Sym)
Definition TapiFile.cpp:26
MCAsmInfoELF(const MCTargetOptions &Options)
StringRef InternalSymbolPrefix
For internal use by compiler and assembler, not meant to be visible externally.
Definition MCAsmInfo.h:160
bool usesSunStyleELFSectionSwitchSyntax() const
Definition MCAsmInfo.h:511
MCAsmInfo(const MCTargetOptions &Options)
Definition MCAsmInfo.cpp:44
const char * WeakRefDirective
This directive, if non-null, is used to declare a global as being a weak undefined symbol.
Definition MCAsmInfo.h:327
StringRef PrivateLabelPrefix
This prefix is used for labels for basic blocks. Defaults to "L".
Definition MCAsmInfo.h:163
StringRef getCommentString() const
Definition MCAsmInfo.h:544
bool HasPreferredAlignment
Definition MCAsmInfo.h:304
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:316
Context object for machine code objects.
Definition MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:550
const Triple & getTargetTriple() const
Definition MCContext.h:397
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
StringRef getName() const
Definition MCSection.h:643
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:675
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:1298
@ SHF_MERGE
Definition ELF.h:1255
@ SHF_STRINGS
Definition ELF.h:1258
@ SHF_AARCH64_PURECODE
Definition ELF.h:1347
@ 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:1303
@ SHF_EXCLUDE
Definition ELF.h:1283
@ SHF_ALLOC
Definition ELF.h:1249
@ SHF_LINK_ORDER
Definition ELF.h:1264
@ SHF_HEX_GPREL
Definition ELF.h:1316
@ SHF_GROUP
Definition ELF.h:1271
@ SHF_SUNW_NODISCARD
Definition ELF.h:1290
@ SHF_X86_64_LARGE
Definition ELF.h:1312
@ SHF_GNU_RETAIN
Definition ELF.h:1280
@ SHF_WRITE
Definition ELF.h:1246
@ SHF_TLS
Definition ELF.h:1274
@ SHF_ARM_PURECODE
Definition ELF.h:1344
@ SHF_EXECINSTR
Definition ELF.h:1252
@ SHT_LLVM_JT_SIZES
Definition ELF.h:1189
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition ELF.h:1179
@ SHT_PROGBITS
Definition ELF.h:1148
@ SHT_LLVM_LINKER_OPTIONS
Definition ELF.h:1176
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition ELF.h:1185
@ SHT_NOBITS
Definition ELF.h:1155
@ SHT_LLVM_ODRTAB
Definition ELF.h:1175
@ SHT_LLVM_OFFLOADING
Definition ELF.h:1187
@ SHT_LLVM_LTO
Definition ELF.h:1188
@ SHT_LLVM_CALL_GRAPH
Definition ELF.h:1191
@ SHT_MIPS_DWARF
Definition ELF.h:1227
@ SHT_PREINIT_ARRAY
Definition ELF.h:1161
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1186
@ SHT_LLVM_CFI_JUMP_TABLE
Definition ELF.h:1190
@ SHT_INIT_ARRAY
Definition ELF.h:1159
@ SHT_NOTE
Definition ELF.h:1154
@ SHT_X86_64_UNWIND
Definition ELF.h:1223
@ SHT_LLVM_SYMPART
Definition ELF.h:1181
@ SHT_FINI_ARRAY
Definition ELF.h:1160
This is an optimization pass for GlobalISel generic memory operations.