LLVM 19.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 const MCExpr *Subsection) const {
57 OS << '\t' << getName();
58 if (Subsection) {
59 OS << '\t';
60 Subsection->print(OS, &MAI);
61 }
62 OS << '\n';
63 return;
64 }
65
66 OS << "\t.section\t";
68
69 // Handle the weird solaris syntax if desired.
71 !(Flags & ELF::SHF_MERGE)) {
72 if (Flags & ELF::SHF_ALLOC)
73 OS << ",#alloc";
74 if (Flags & ELF::SHF_EXECINSTR)
75 OS << ",#execinstr";
76 if (Flags & ELF::SHF_WRITE)
77 OS << ",#write";
78 if (Flags & ELF::SHF_EXCLUDE)
79 OS << ",#exclude";
80 if (Flags & ELF::SHF_TLS)
81 OS << ",#tls";
82 OS << '\n';
83 return;
84 }
85
86 OS << ",\"";
87 if (Flags & ELF::SHF_ALLOC)
88 OS << 'a';
89 if (Flags & ELF::SHF_EXCLUDE)
90 OS << 'e';
91 if (Flags & ELF::SHF_EXECINSTR)
92 OS << 'x';
93 if (Flags & ELF::SHF_WRITE)
94 OS << 'w';
95 if (Flags & ELF::SHF_MERGE)
96 OS << 'M';
97 if (Flags & ELF::SHF_STRINGS)
98 OS << 'S';
99 if (Flags & ELF::SHF_TLS)
100 OS << 'T';
101 if (Flags & ELF::SHF_LINK_ORDER)
102 OS << 'o';
103 if (Flags & ELF::SHF_GROUP)
104 OS << 'G';
105 if (Flags & ELF::SHF_GNU_RETAIN)
106 OS << 'R';
107
108 // If there are os-specific flags, print them.
109 if (T.isOSSolaris())
110 if (Flags & ELF::SHF_SUNW_NODISCARD)
111 OS << 'R';
112
113 // If there are target-specific flags, print them.
114 Triple::ArchType Arch = T.getArch();
115 if (Arch == Triple::xcore) {
116 if (Flags & ELF::XCORE_SHF_CP_SECTION)
117 OS << 'c';
118 if (Flags & ELF::XCORE_SHF_DP_SECTION)
119 OS << 'd';
120 } else if (T.isARM() || T.isThumb()) {
121 if (Flags & ELF::SHF_ARM_PURECODE)
122 OS << 'y';
123 } else if (Arch == Triple::hexagon) {
124 if (Flags & ELF::SHF_HEX_GPREL)
125 OS << 's';
126 } else if (Arch == Triple::x86_64) {
127 if (Flags & ELF::SHF_X86_64_LARGE)
128 OS << 'l';
129 }
130
131 OS << '"';
132
133 OS << ',';
134
135 // If comment string is '@', e.g. as on ARM - use '%' instead
136 if (MAI.getCommentString()[0] == '@')
137 OS << '%';
138 else
139 OS << '@';
140
142 OS << "init_array";
143 else if (Type == ELF::SHT_FINI_ARRAY)
144 OS << "fini_array";
145 else if (Type == ELF::SHT_PREINIT_ARRAY)
146 OS << "preinit_array";
147 else if (Type == ELF::SHT_NOBITS)
148 OS << "nobits";
149 else if (Type == ELF::SHT_NOTE)
150 OS << "note";
151 else if (Type == ELF::SHT_PROGBITS)
152 OS << "progbits";
153 else if (Type == ELF::SHT_X86_64_UNWIND)
154 OS << "unwind";
155 else if (Type == ELF::SHT_MIPS_DWARF)
156 // Print hex value of the flag while we do not have
157 // any standard symbolic representation of the flag.
158 OS << "0x7000001e";
159 else if (Type == ELF::SHT_LLVM_ODRTAB)
160 OS << "llvm_odrtab";
162 OS << "llvm_linker_options";
164 OS << "llvm_call_graph_profile";
166 OS << "llvm_dependent_libraries";
167 else if (Type == ELF::SHT_LLVM_SYMPART)
168 OS << "llvm_sympart";
170 OS << "llvm_bb_addr_map";
172 OS << "llvm_bb_addr_map_v0";
173 else if (Type == ELF::SHT_LLVM_OFFLOADING)
174 OS << "llvm_offloading";
175 else if (Type == ELF::SHT_LLVM_LTO)
176 OS << "llvm_lto";
177 else
178 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
179 " for section " + getName());
180
181 if (EntrySize) {
182 assert(Flags & ELF::SHF_MERGE);
183 OS << "," << EntrySize;
184 }
185
186 if (Flags & ELF::SHF_LINK_ORDER) {
187 OS << ",";
188 if (LinkedToSym)
189 printName(OS, LinkedToSym->getName());
190 else
191 OS << '0';
192 }
193
194 if (Flags & ELF::SHF_GROUP) {
195 OS << ",";
196 printName(OS, Group.getPointer()->getName());
197 if (isComdat())
198 OS << ",comdat";
199 }
200
201 if (isUnique())
202 OS << ",unique," << UniqueID;
203
204 OS << '\n';
205
206 if (Subsection) {
207 OS << "\t.subsection\t";
208 Subsection->print(OS, &MAI);
209 OS << '\n';
210 }
211}
212
214 return getFlags() & ELF::SHF_EXECINSTR;
215}
216
218 return getType() == ELF::SHT_NOBITS;
219}
220
221StringRef 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:613
StringRef getCommentString() const
Definition: MCAsmInfo.h:651
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:126
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
unsigned getFlags() const
Definition: MCSectionELF.h:73
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *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:86
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
bool useCodeAlign() const override
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
unsigned getType() const
Definition: MCSectionELF.h:72
bool isComdat() const
Definition: MCSectionELF.h:77
StringRef Name
Definition: MCSection.h:113
StringRef getName() const
Definition: MCSection.h:124
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:50
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
@ 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:1206
@ SHF_MERGE
Definition: ELF.h:1163
@ SHF_STRINGS
Definition: ELF.h:1166
@ 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:1211
@ SHF_EXCLUDE
Definition: ELF.h:1191
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_LINK_ORDER
Definition: ELF.h:1172
@ SHF_HEX_GPREL
Definition: ELF.h:1224
@ SHF_GROUP
Definition: ELF.h:1179
@ SHF_SUNW_NODISCARD
Definition: ELF.h:1198
@ SHF_X86_64_LARGE
Definition: ELF.h:1220
@ SHF_GNU_RETAIN
Definition: ELF.h:1188
@ SHF_WRITE
Definition: ELF.h:1154
@ SHF_TLS
Definition: ELF.h:1182
@ SHF_ARM_PURECODE
Definition: ELF.h:1252
@ SHF_EXECINSTR
Definition: ELF.h:1160
@ SHT_LLVM_BB_ADDR_MAP_V0
Definition: ELF.h:1096
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:1091
@ SHT_PROGBITS
Definition: ELF.h:1063
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:1088
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:1099
@ SHT_NOBITS
Definition: ELF.h:1070
@ SHT_LLVM_ODRTAB
Definition: ELF.h:1087
@ SHT_LLVM_OFFLOADING
Definition: ELF.h:1101
@ SHT_LLVM_LTO
Definition: ELF.h:1102
@ SHT_MIPS_DWARF
Definition: ELF.h:1135
@ SHT_PREINIT_ARRAY
Definition: ELF.h:1076
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1100
@ SHT_INIT_ARRAY
Definition: ELF.h:1074
@ SHT_NOTE
Definition: ELF.h:1069
@ SHT_X86_64_UNWIND
Definition: ELF.h:1131
@ SHT_LLVM_SYMPART
Definition: ELF.h:1093
@ SHT_FINI_ARRAY
Definition: ELF.h:1075
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156