LLVM 24.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
28static void printName(raw_ostream &OS, StringRef Name) {
29 if (Name.find_first_not_of("0123456789_."
30 "abcdefghijklmnopqrstuvwxyz"
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
32 OS << Name;
33 return;
34 }
35 OS << '"';
36 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
37 if (*B == '"') // Unquoted "
38 OS << "\\\"";
39 else if (*B != '\\') // Neither " or backslash
40 OS << *B;
41 else if (B + 1 == E) // Trailing backslash
42 OS << "\\\\";
43 else {
44 OS << B[0] << B[1]; // Quoted character
45 ++B;
46 }
47 }
48 OS << '"';
49}
50
51static void printSwitchToSection(const MCAsmInfo &MAI, const MCSection &Section,
52 uint32_t Subsection, const Triple &T,
53 raw_ostream &OS) {
54 auto &Sec = static_cast<const MCSectionELF &>(Section);
55 unsigned Type = Sec.getType();
56 unsigned Flags = Sec.getFlags();
57 if (!Sec.isUnique() && MAI.isSectionDirective(Sec.getName())) {
58 OS << '\t' << Sec.getName();
59 if (Subsection)
60 OS << '\t' << Subsection;
61 OS << '\n';
62 return;
63 }
64
65 OS << "\t.section\t";
66 printName(OS, Sec.getName());
67
68 // Handle the weird solaris syntax if desired.
69 if (MAI.usesSunStyleELFSectionSwitchSyntax() && !(Flags & ELF::SHF_MERGE)) {
70 if (Flags & ELF::SHF_ALLOC)
71 OS << ",#alloc";
72 if (Flags & ELF::SHF_EXECINSTR)
73 OS << ",#execinstr";
74 if (Flags & ELF::SHF_WRITE)
75 OS << ",#write";
76 if (Flags & ELF::SHF_EXCLUDE)
77 OS << ",#exclude";
78 if (Flags & ELF::SHF_TLS)
79 OS << ",#tls";
80 OS << '\n';
81 return;
82 }
83
84 OS << ",\"";
85 if (Flags & ELF::SHF_ALLOC)
86 OS << 'a';
87 if (Flags & ELF::SHF_EXCLUDE)
88 OS << 'e';
89 if (Flags & ELF::SHF_EXECINSTR)
90 OS << 'x';
91 if (Flags & ELF::SHF_WRITE)
92 OS << 'w';
93 if (Flags & ELF::SHF_MERGE)
94 OS << 'M';
95 if (Flags & ELF::SHF_STRINGS)
96 OS << 'S';
97 if (Flags & ELF::SHF_TLS)
98 OS << 'T';
99 if (Flags & ELF::SHF_LINK_ORDER)
100 OS << 'o';
101 if (Flags & ELF::SHF_GROUP)
102 OS << 'G';
103 if (Flags & ELF::SHF_GNU_RETAIN)
104 OS << 'R';
105
106 // If there are os-specific flags, print them.
107 if (T.isOSSolaris())
108 if (Flags & ELF::SHF_SUNW_NODISCARD)
109 OS << 'R';
110
111 // If there are tarSec.get-specific flags, print them.
112 Triple::ArchType Arch = T.getArch();
113 if (Arch == Triple::xcore) {
114 if (Flags & ELF::XCORE_SHF_CP_SECTION)
115 OS << 'c';
116 if (Flags & ELF::XCORE_SHF_DP_SECTION)
117 OS << 'd';
118 } else if (T.isARM() || T.isThumb()) {
119 if (Flags & ELF::SHF_ARM_PURECODE)
120 OS << 'y';
121 } else if (T.isAArch64()) {
122 if (Flags & ELF::SHF_AARCH64_PURECODE)
123 OS << 'y';
124 } else if (Arch == Triple::hexagon) {
125 if (Flags & ELF::SHF_HEX_GPREL)
126 OS << 's';
127 } else if (Arch == Triple::x86_64) {
128 if (Flags & ELF::SHF_X86_64_LARGE)
129 OS << 'l';
130 }
131
132 OS << '"';
133
134 OS << ',';
135
136 // If comment string is '@', e.g. as on ARM - use '%' instead
137 if (MAI.getCommentString()[0] == '@')
138 OS << '%';
139 else
140 OS << '@';
141
143 OS << "init_array";
144 else if (Type == ELF::SHT_FINI_ARRAY)
145 OS << "fini_array";
146 else if (Type == ELF::SHT_PREINIT_ARRAY)
147 OS << "preinit_array";
148 else if (Type == ELF::SHT_NOBITS)
149 OS << "nobits";
150 else if (Type == ELF::SHT_NOTE)
151 OS << "note";
152 else if (Type == ELF::SHT_PROGBITS)
153 OS << "progbits";
154 else if (Type == ELF::SHT_X86_64_UNWIND)
155 OS << "unwind";
156 else if (Type == ELF::SHT_MIPS_DWARF)
157 // Print hex value of the flag while we do not have
158 // any standard symbolic representation of the flag.
159 OS << "0x7000001e";
160 else if (Type == ELF::SHT_LLVM_ODRTAB)
161 OS << "llvm_odrtab";
163 OS << "llvm_linker_options";
165 OS << "llvm_call_graph_profile";
167 OS << "llvm_dependent_libraries";
168 else if (Type == ELF::SHT_LLVM_SYMPART)
169 OS << "llvm_sympart";
171 OS << "llvm_bb_addr_map";
172 else if (Type == ELF::SHT_LLVM_OFFLOADING)
173 OS << "llvm_offloading";
174 else if (Type == ELF::SHT_LLVM_LTO)
175 OS << "llvm_lto";
176 else if (Type == ELF::SHT_LLVM_JT_SIZES)
177 OS << "llvm_jt_sizes";
179 OS << "llvm_cfi_jump_table";
180 else if (Type == ELF::SHT_LLVM_CALL_GRAPH)
181 OS << "llvm_call_graph";
182 else if (Type == ELF::SHT_LLVM_DYNDBG_ELF)
183 OS << "llvm_dyndbg_elf";
184 else
185 OS << "0x" << Twine::utohexstr(Type);
186
187 if (unsigned ES = Sec.getEntrySize()) {
189 OS << "," << ES;
190 }
191
192 if (Flags & ELF::SHF_LINK_ORDER) {
193 OS << ",";
194 if (const MCSymbol *Sym = Sec.getLinkedToSymbol())
195 printName(OS, Sym->getName());
196 else
197 OS << '0';
198 }
199
200 if (Flags & ELF::SHF_GROUP) {
201 OS << ",";
202 printName(OS, Sec.getGroup()->getName());
203 if (Sec.isComdat())
204 OS << ",comdat";
205 }
206
207 if (Sec.isUnique())
208 OS << ",unique," << Sec.getUniqueID();
209
210 OS << '\n';
211
212 if (Subsection) {
213 OS << "\t.subsection\t" << Subsection;
214 OS << '\n';
215 }
216}
217
219 : MCAsmInfo(Options) {
220 // Solaris doesn't know/doesn't care about .note.GNU-stack sections, so
221 // don't emit them.
222 EmitELFStackSection = !TT.isOSSolaris();
223
224 HasIdentDirective = true;
226 WeakRefDirective = "\t.weak\t";
229 SectionDirectives.insert(".text");
230 SectionDirectives.insert(".data");
231}
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 printSwitchToSection(const MCAsmInfo &MAI, const MCSection &Section, uint32_t Subsection, const Triple &T, raw_ostream &OS)
static void printName(raw_ostream &OS, StringRef Name)
#define T
MCAsmInfoELF(const Triple &TT, const MCTargetOptions &Options)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:69
StringRef InternalSymbolPrefix
For internal use by compiler and assembler, not meant to be visible externally.
Definition MCAsmInfo.h:169
bool usesSunStyleELFSectionSwitchSyntax() const
Definition MCAsmInfo.h:561
MCAsmInfo(const MCTargetOptions &Options)
Definition MCAsmInfo.cpp:28
void printSwitchToSection(const MCSection &Sec, uint32_t Subsection, const Triple &T, raw_ostream &OS) const
Definition MCAsmInfo.h:555
const char * WeakRefDirective
This directive, if non-null, is used to declare a global as being a weak undefined symbol.
Definition MCAsmInfo.h:341
SmallDenseSet< StringRef, 8 > SectionDirectives
Sections whose name also is supported as a directive without an explicit ".section" directive.
Definition MCAsmInfo.h:475
StringRef getCommentString() const
Definition MCAsmInfo.h:587
bool HasPreferredAlignment
Definition MCAsmInfo.h:318
bool HasIdentDirective
True if the target has a .ident directive, this is true for ELF targets.
Definition MCAsmInfo.h:330
bool EmitELFStackSection
Whether a section indicating a (non-)executable stack should be emitted.
Definition MCAsmInfo.h:106
void(* PrintSwitchToSectionFn)(const MCAsmInfo &, const MCSection &, uint32_t Subsection, const Triple &, raw_ostream &)
Definition MCAsmInfo.h:276
bool isSectionDirective(StringRef SecName) const
Definition MCAsmInfo.h:551
This represents a section on linux, lots of unix variants and some bare metal systems.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:577
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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:1147
@ SHF_MERGE
Definition ELF.h:1104
@ SHF_STRINGS
Definition ELF.h:1107
@ SHF_AARCH64_PURECODE
Definition ELF.h:1196
@ 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:1152
@ SHF_EXCLUDE
Definition ELF.h:1132
@ SHF_ALLOC
Definition ELF.h:1098
@ SHF_LINK_ORDER
Definition ELF.h:1113
@ SHF_HEX_GPREL
Definition ELF.h:1165
@ SHF_GROUP
Definition ELF.h:1120
@ SHF_SUNW_NODISCARD
Definition ELF.h:1139
@ SHF_X86_64_LARGE
Definition ELF.h:1161
@ SHF_GNU_RETAIN
Definition ELF.h:1129
@ SHF_WRITE
Definition ELF.h:1095
@ SHF_TLS
Definition ELF.h:1123
@ SHF_ARM_PURECODE
Definition ELF.h:1193
@ SHF_EXECINSTR
Definition ELF.h:1101
@ SHT_LLVM_JT_SIZES
Definition ELF.h:1037
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition ELF.h:1027
@ SHT_PROGBITS
Definition ELF.h:996
@ SHT_LLVM_LINKER_OPTIONS
Definition ELF.h:1024
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition ELF.h:1033
@ SHT_NOBITS
Definition ELF.h:1003
@ SHT_LLVM_ODRTAB
Definition ELF.h:1023
@ SHT_LLVM_OFFLOADING
Definition ELF.h:1035
@ SHT_LLVM_LTO
Definition ELF.h:1036
@ SHT_LLVM_CALL_GRAPH
Definition ELF.h:1039
@ SHT_MIPS_DWARF
Definition ELF.h:1076
@ SHT_PREINIT_ARRAY
Definition ELF.h:1009
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1034
@ SHT_LLVM_CFI_JUMP_TABLE
Definition ELF.h:1038
@ SHT_INIT_ARRAY
Definition ELF.h:1007
@ SHT_LLVM_DYNDBG_ELF
Definition ELF.h:1040
@ SHT_NOTE
Definition ELF.h:1002
@ SHT_X86_64_UNWIND
Definition ELF.h:1072
@ SHT_LLVM_SYMPART
Definition ELF.h:1029
@ SHT_FINI_ARRAY
Definition ELF.h:1008
This is an optimization pass for GlobalISel generic memory operations.