LLVM 20.0.0git
MCSectionELF.cpp
Go to the documentation of this file.
1//===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
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
11#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCExpr.h"
16#include <cassert>
17
18using namespace llvm;
19
20// Decides whether a '.section' directive
21// should be printed before the section name.
23 const MCAsmInfo &MAI) const {
24 if (isUnique())
25 return false;
26
28}
29
31 if (Name.find_first_not_of("0123456789_."
32 "abcdefghijklmnopqrstuvwxyz"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
34 OS << Name;
35 return;
36 }
37 OS << '"';
38 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
39 if (*B == '"') // Unquoted "
40 OS << "\\\"";
41 else if (*B != '\\') // Neither " or backslash
42 OS << *B;
43 else if (B + 1 == E) // Trailing backslash
44 OS << "\\\\";
45 else {
46 OS << B[0] << B[1]; // Quoted character
47 ++B;
48 }
49 }
50 OS << '"';
51}
52
55 uint32_t Subsection) const {
57 OS << '\t' << getName();
58 if (Subsection)
59 OS << '\t' << Subsection;
60 OS << '\n';
61 return;
62 }
63
64 OS << "\t.section\t";
66
67 // Handle the weird solaris syntax if desired.
69 !(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 target-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 (Arch == Triple::hexagon) {
122 if (Flags & ELF::SHF_HEX_GPREL)
123 OS << 's';
124 } else if (Arch == Triple::x86_64) {
125 if (Flags & ELF::SHF_X86_64_LARGE)
126 OS << 'l';
127 }
128
129 OS << '"';
130
131 OS << ',';
132
133 // If comment string is '@', e.g. as on ARM - use '%' instead
134 if (MAI.getCommentString()[0] == '@')
135 OS << '%';
136 else
137 OS << '@';
138
140 OS << "init_array";
141 else if (Type == ELF::SHT_FINI_ARRAY)
142 OS << "fini_array";
143 else if (Type == ELF::SHT_PREINIT_ARRAY)
144 OS << "preinit_array";
145 else if (Type == ELF::SHT_NOBITS)
146 OS << "nobits";
147 else if (Type == ELF::SHT_NOTE)
148 OS << "note";
149 else if (Type == ELF::SHT_PROGBITS)
150 OS << "progbits";
151 else if (Type == ELF::SHT_X86_64_UNWIND)
152 OS << "unwind";
153 else if (Type == ELF::SHT_MIPS_DWARF)
154 // Print hex value of the flag while we do not have
155 // any standard symbolic representation of the flag.
156 OS << "0x7000001e";
157 else if (Type == ELF::SHT_LLVM_ODRTAB)
158 OS << "llvm_odrtab";
160 OS << "llvm_linker_options";
162 OS << "llvm_call_graph_profile";
164 OS << "llvm_dependent_libraries";
165 else if (Type == ELF::SHT_LLVM_SYMPART)
166 OS << "llvm_sympart";
168 OS << "llvm_bb_addr_map";
170 OS << "llvm_bb_addr_map_v0";
171 else if (Type == ELF::SHT_LLVM_OFFLOADING)
172 OS << "llvm_offloading";
173 else if (Type == ELF::SHT_LLVM_LTO)
174 OS << "llvm_lto";
175 else if (Type == ELF::SHT_LLVM_JT_SIZES)
176 OS << "llvm_jt_sizes";
177 else
178 OS << "0x" << Twine::utohexstr(Type);
179
180 if (EntrySize) {
181 assert(Flags & ELF::SHF_MERGE);
182 OS << "," << EntrySize;
183 }
184
185 if (Flags & ELF::SHF_LINK_ORDER) {
186 OS << ",";
187 if (LinkedToSym)
188 printName(OS, LinkedToSym->getName());
189 else
190 OS << '0';
191 }
192
193 if (Flags & ELF::SHF_GROUP) {
194 OS << ",";
195 printName(OS, Group.getPointer()->getName());
196 if (isComdat())
197 OS << ",comdat";
198 }
199
200 if (isUnique())
201 OS << ",unique," << UniqueID;
202
203 OS << '\n';
204
205 if (Subsection) {
206 OS << "\t.subsection\t" << Subsection;
207 OS << '\n';
208 }
209}
210
212 return getFlags() & ELF::SHF_EXECINSTR;
213}
214
215StringRef MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS"; }
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::string Name
static void printName(raw_ostream &OS, StringRef Name)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool usesSunStyleELFSectionSwitchSyntax() const
Definition: MCAsmInfo.h:579
StringRef getCommentString() const
Definition: MCAsmInfo.h:615
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:122
unsigned getFlags() const
Definition: MCSectionELF.h:81
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, uint32_t Subsection) const override
StringRef getVirtualSectionKind() const override
bool shouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const
Decides whether a '.section' directive should be printed before the section name.
bool isUnique() const
Definition: MCSectionELF.h:93
bool useCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
bool isComdat() const
Definition: MCSectionELF.h:85
StringRef Name
Definition: MCSection.h:119
StringRef getName() const
Definition: MCSection.h:130
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ SHT_LLVM_BB_ADDR_MAP_V0
Definition: ELF.h:1126
@ SHT_LLVM_JT_SIZES
Definition: ELF.h:1133
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:1121
@ SHT_PROGBITS
Definition: ELF.h:1090
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:1118
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:1129
@ SHT_NOBITS
Definition: ELF.h:1097
@ SHT_LLVM_ODRTAB
Definition: ELF.h:1117
@ SHT_LLVM_OFFLOADING
Definition: ELF.h:1131
@ SHT_LLVM_LTO
Definition: ELF.h:1132
@ SHT_MIPS_DWARF
Definition: ELF.h:1166
@ SHT_PREINIT_ARRAY
Definition: ELF.h:1103
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1130
@ SHT_INIT_ARRAY
Definition: ELF.h:1101
@ SHT_NOTE
Definition: ELF.h:1096
@ SHT_X86_64_UNWIND
Definition: ELF.h:1162
@ SHT_LLVM_SYMPART
Definition: ELF.h:1123
@ SHT_FINI_ARRAY
Definition: ELF.h:1102
@ 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:1237
@ SHF_MERGE
Definition: ELF.h:1194
@ SHF_STRINGS
Definition: ELF.h:1197
@ 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:1242
@ SHF_EXCLUDE
Definition: ELF.h:1222
@ SHF_ALLOC
Definition: ELF.h:1188
@ SHF_LINK_ORDER
Definition: ELF.h:1203
@ SHF_HEX_GPREL
Definition: ELF.h:1255
@ SHF_GROUP
Definition: ELF.h:1210
@ SHF_SUNW_NODISCARD
Definition: ELF.h:1229
@ SHF_X86_64_LARGE
Definition: ELF.h:1251
@ SHF_GNU_RETAIN
Definition: ELF.h:1219
@ SHF_WRITE
Definition: ELF.h:1185
@ SHF_TLS
Definition: ELF.h:1213
@ SHF_ARM_PURECODE
Definition: ELF.h:1283
@ SHF_EXECINSTR
Definition: ELF.h:1191
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18