LLVM 22.0.0git
MCSectionMachO.cpp
Go to the documentation of this file.
1//===- lib/MC/MCSectionMachO.cpp - MachO 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/SectionKind.h"
13
14namespace llvm {
15class MCAsmInfo;
16class MCExpr;
17class MCSymbol;
18class Triple;
19} // namespace llvm
20
21using namespace llvm;
22
23/// SectionTypeDescriptors - These are strings that describe the various section
24/// types. This *must* be kept in order with and stay synchronized with the
25/// section type list.
26static constexpr struct {
29 {StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00
30 {StringLiteral("zerofill"), StringLiteral("S_ZEROFILL")}, // 0x01
31 {StringLiteral("cstring_literals"),
32 StringLiteral("S_CSTRING_LITERALS")}, // 0x02
33 {StringLiteral("4byte_literals"),
34 StringLiteral("S_4BYTE_LITERALS")}, // 0x03
35 {StringLiteral("8byte_literals"),
36 StringLiteral("S_8BYTE_LITERALS")}, // 0x04
37 {StringLiteral("literal_pointers"),
38 StringLiteral("S_LITERAL_POINTERS")}, // 0x05
39 {StringLiteral("non_lazy_symbol_pointers"),
40 StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06
41 {StringLiteral("lazy_symbol_pointers"),
42 StringLiteral("S_LAZY_SYMBOL_POINTERS")}, // 0x07
43 {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08
44 {StringLiteral("mod_init_funcs"),
45 StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09
46 {StringLiteral("mod_term_funcs"),
47 StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, // 0x0A
48 {StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, // 0x0B
49 {StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C
50 {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, // 0x0D
51 {StringLiteral("16byte_literals"),
52 StringLiteral("S_16BYTE_LITERALS")}, // 0x0E
53 {StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F
54 {StringLiteral("") /*FIXME??*/,
55 StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10
56 {StringLiteral("thread_local_regular"),
57 StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11
58 {StringLiteral("thread_local_zerofill"),
59 StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12
60 {StringLiteral("thread_local_variables"),
61 StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13
62 {StringLiteral("thread_local_variable_pointers"),
63 StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14
64 {StringLiteral("thread_local_init_function_pointers"),
65 StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15
66 {StringLiteral("") /* linker-synthesized */,
67 StringLiteral("S_INIT_FUNC_OFFSETS")}, // 0x16
68};
69
70/// SectionAttrDescriptors - This is an array of descriptors for section
71/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
72/// by attribute, instead it is searched.
73static constexpr struct {
74 unsigned AttrFlag;
77#define ENTRY(ASMNAME, ENUM) \
78 { MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
79ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
80ENTRY("no_toc", S_ATTR_NO_TOC)
81ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
82ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
83ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
84ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
85ENTRY("debug", S_ATTR_DEBUG)
87ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC)
88ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
89#undef ENTRY
90 { 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size
91};
92
93MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
94 unsigned TAA, unsigned reserved2, SectionKind K,
95 MCSymbol *Begin)
96 : MCSection(Section, K.isText(),
97 MachO::isVirtualSection(TAA & MachO::SECTION_TYPE), Begin),
98 TypeAndAttributes(TAA), Reserved2(reserved2) {
99 assert(Segment.size() <= 16 && Section.size() <= 16 &&
100 "Segment or section string too long");
101 for (unsigned i = 0; i != 16; ++i) {
102 if (i < Segment.size())
103 SegmentName[i] = Segment[i];
104 else
105 SegmentName[i] = 0;
106 }
107}
108
110 const Triple &T,
111 raw_ostream &OS) const {
112 auto &Sec = static_cast<const MCSectionMachO &>(Section);
113 OS << "\t.section\t" << Sec.getSegmentName() << ',' << Sec.getName();
114
115 // Get the section type and attributes.
116 unsigned TAA = Sec.getTypeAndAttributes();
117 if (TAA == 0) {
118 OS << '\n';
119 return;
120 }
121
122 MachO::SectionType SectionType = Sec.getType();
123 assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
124 "Invalid SectionType specified!");
125
126 if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
127 OS << ',';
128 OS << SectionTypeDescriptors[SectionType].AssemblerName;
129 } else {
130 // If we have no name for the attribute, stop here.
131 OS << '\n';
132 return;
133 }
134
135 // If we don't have any attributes, we're done.
136 unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
137 if (SectionAttrs == 0) {
138 // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
139 // the attribute specifier.
140 if (Sec.Reserved2 != 0)
141 OS << ",none," << Sec.Reserved2;
142 OS << '\n';
143 return;
144 }
145
146 // Check each attribute to see if we have it.
147 char Separator = ',';
148 for (unsigned i = 0;
149 SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
150 ++i) {
151 // Check to see if we have this attribute.
152 if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
153 continue;
154
155 // Yep, clear it and print it.
156 SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
157
158 OS << Separator;
160 OS << SectionAttrDescriptors[i].AssemblerName;
161 else
162 OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
163 Separator = '+';
164 }
165
166 assert(SectionAttrs == 0 && "Unknown section attributes!");
167
168 // If we have a S_SYMBOL_STUBS size specified, print it.
169 if (Sec.Reserved2 != 0)
170 OS << ',' << Sec.Reserved2;
171 OS << '\n';
172}
173
174/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
175/// This is a string that can appear after a .section directive in a mach-o
176/// flavored .s file. If successful, this fills in the specified Out
177/// parameters and returns an empty string. When an invalid section
178/// specifier is present, this returns a string indicating the problem.
180 StringRef &Segment, // Out.
181 StringRef &Section, // Out.
182 unsigned &TAA, // Out.
183 bool &TAAParsed, // Out.
184 unsigned &StubSize) { // Out.
185 TAAParsed = false;
186
188 Spec.split(SplitSpec, ',');
189 // Remove leading and trailing whitespace.
190 auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
191 return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
192 };
193 Segment = GetEmptyOrTrim(0);
194 Section = GetEmptyOrTrim(1);
195 StringRef SectionType = GetEmptyOrTrim(2);
196 StringRef Attrs = GetEmptyOrTrim(3);
197 StringRef StubSizeStr = GetEmptyOrTrim(4);
198
199 // Verify that the section is present.
200 if (Section.empty())
202 "mach-o section specifier requires a segment "
203 "and section separated by a comma");
204
205 // Verify that the section is not too long.
206 if (Section.size() > 16)
208 "mach-o section specifier requires a section "
209 "whose length is between 1 and 16 characters");
210
211 // If there is no comma after the section, we're done.
212 TAA = 0;
213 StubSize = 0;
214 if (SectionType.empty())
215 return Error::success();
216
217 // Figure out which section type it is.
218 auto TypeDescriptor =
220 [&](decltype(*SectionTypeDescriptors) &Descriptor) {
221 return SectionType == Descriptor.AssemblerName;
222 });
223
224 // If we didn't find the section type, reject it.
225 if (TypeDescriptor == std::end(SectionTypeDescriptors))
227 "mach-o section specifier uses an unknown "
228 "section type");
229
230 // Remember the TypeID.
231 TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
232 TAAParsed = true;
233
234 // If we have no comma after the section type, there are no attributes.
235 if (Attrs.empty()) {
236 // S_SYMBOL_STUBS always require a symbol stub size specifier.
237 if (TAA == MachO::S_SYMBOL_STUBS)
239 "mach-o section specifier of type "
240 "'symbol_stubs' requires a size specifier");
241 return Error::success();
242 }
243
244 // The attribute list is a '+' separated list of attributes.
245 SmallVector<StringRef, 1> SectionAttrs;
246 Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
247
248 for (StringRef &SectionAttr : SectionAttrs) {
249 auto AttrDescriptorI =
251 [&](decltype(*SectionAttrDescriptors) &Descriptor) {
252 return SectionAttr.trim() == Descriptor.AssemblerName;
253 });
254 if (AttrDescriptorI == std::end(SectionAttrDescriptors))
256 "mach-o section specifier has invalid "
257 "attribute");
258
259 TAA |= AttrDescriptorI->AttrFlag;
260 }
261
262 // Okay, we've parsed the section attributes, see if we have a stub size spec.
263 if (StubSizeStr.empty()) {
264 // S_SYMBOL_STUBS always require a symbol stub size specifier.
265 if (TAA == MachO::S_SYMBOL_STUBS)
267 "mach-o section specifier of type "
268 "'symbol_stubs' requires a size specifier");
269 return Error::success();
270 }
271
272 // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
275 "mach-o section specifier cannot have a stub "
276 "size specified because it does not have type "
277 "'symbol_stubs'");
278
279 // Convert the stub size from a string to an integer.
280 if (StubSizeStr.getAsInteger(0, StubSize))
282 "mach-o section specifier has a malformed "
283 "stub size");
284
285 return Error::success();
286}
287
289 auto *L = curFragList();
290 if (L->Tail)
291 Atoms.resize(L->Tail->getLayoutOrder() + 1);
292}
293
294const MCSymbol *MCSectionMachO::getAtom(size_t I) const {
295 return I < Atoms.size() ? Atoms[I] : nullptr;
296}
297
298void MCSectionMachO::setAtom(size_t I, const MCSymbol *Sym) { Atoms[I] = Sym; }
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Symbol * Sym
Definition: ELF_riscv.cpp:479
unsigned AttrFlag
static constexpr struct @516 SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1]
SectionTypeDescriptors - These are strings that describe the various section types.
static constexpr struct @517 SectionAttrDescriptors[]
SectionAttrDescriptors - This is an array of descriptors for section attributes.
#define ENTRY(ASMNAME, ENUM)
StringLiteral EnumName
StringLiteral AssemblerName
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
void printSwitchToSection(const MCSection &, uint32_t, const Triple &, raw_ostream &) const final
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
This represents a section on a Mach-O system (used by Mac OS X).
static Error ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
Parse the section specifier indicated by "Spec".
void setAtom(size_t I, const MCSymbol *Sym)
const MCSymbol * getAtom(size_t I) const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
FragList * curFragList() const
Definition: MCSection.h:603
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
size_t size() const
Definition: SmallVector.h:79
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:862
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:480
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ SECTION_ATTRIBUTES
Definition: MachO.h:115
@ SECTION_TYPE
Definition: MachO.h:114
bool isVirtualSection(uint8_t type)
Definition: MachO.h:595
@ S_ATTR_SOME_INSTRUCTIONS
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
Definition: MachO.h:213
@ S_ATTR_EXT_RELOC
S_ATTR_EXT_RELOC - Section has external relocation entries.
Definition: MachO.h:215
@ S_ATTR_DEBUG
S_ATTR_DEBUG - A debug section.
Definition: MachO.h:207
@ S_ATTR_NO_DEAD_STRIP
S_ATTR_NO_DEAD_STRIP - No dead stripping.
Definition: MachO.h:200
@ S_ATTR_LOC_RELOC
S_ATTR_LOC_RELOC - Section has local relocation entries.
Definition: MachO.h:217
@ S_ATTR_NO_TOC
S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be in a ranlib table of contents.
Definition: MachO.h:195
@ S_ATTR_LIVE_SUPPORT
S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
Definition: MachO.h:202
@ S_ATTR_PURE_INSTRUCTIONS
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
Definition: MachO.h:192
@ S_ATTR_SELF_MODIFYING_CODE
S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by dyld.
Definition: MachO.h:205
@ S_ATTR_STRIP_STATIC_SYMS
S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section in files with the MY_DYLDLINK f...
Definition: MachO.h:198
SectionType
These are the section type and attributes fields.
Definition: MachO.h:122
@ LAST_KNOWN_SECTION_TYPE
Definition: MachO.h:183
@ S_SYMBOL_STUBS
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
Definition: MachO.h:144
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1777