2009-08-11 02:15:01 +08:00
|
|
|
//===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-08-11 02:15:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2022-02-08 22:32:21 +08:00
|
|
|
#include "llvm/MC/SectionKind.h"
|
2009-08-11 02:15:01 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2022-02-08 22:32:21 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MCAsmInfo;
|
|
|
|
class MCExpr;
|
|
|
|
class MCSymbol;
|
|
|
|
class Triple;
|
|
|
|
} // namespace llvm
|
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// SectionTypeDescriptors - These are strings that describe the various section
|
|
|
|
/// types. This *must* be kept in order with and stay synchronized with the
|
|
|
|
/// section type list.
|
2017-01-31 02:46:26 +08:00
|
|
|
static constexpr struct {
|
|
|
|
StringLiteral AssemblerName, EnumName;
|
2017-01-31 03:05:09 +08:00
|
|
|
} SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = {
|
|
|
|
{StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00
|
|
|
|
{StringLiteral(""), StringLiteral("S_ZEROFILL")}, // 0x01
|
|
|
|
{StringLiteral("cstring_literals"),
|
|
|
|
StringLiteral("S_CSTRING_LITERALS")}, // 0x02
|
|
|
|
{StringLiteral("4byte_literals"),
|
|
|
|
StringLiteral("S_4BYTE_LITERALS")}, // 0x03
|
|
|
|
{StringLiteral("8byte_literals"),
|
|
|
|
StringLiteral("S_8BYTE_LITERALS")}, // 0x04
|
|
|
|
{StringLiteral("literal_pointers"),
|
|
|
|
StringLiteral("S_LITERAL_POINTERS")}, // 0x05
|
|
|
|
{StringLiteral("non_lazy_symbol_pointers"),
|
|
|
|
StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06
|
|
|
|
{StringLiteral("lazy_symbol_pointers"),
|
|
|
|
StringLiteral("S_LAZY_SYMBOL_POINTERS")}, // 0x07
|
|
|
|
{StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08
|
|
|
|
{StringLiteral("mod_init_funcs"),
|
|
|
|
StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09
|
|
|
|
{StringLiteral("mod_term_funcs"),
|
|
|
|
StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, // 0x0A
|
|
|
|
{StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, // 0x0B
|
|
|
|
{StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C
|
|
|
|
{StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, // 0x0D
|
|
|
|
{StringLiteral("16byte_literals"),
|
|
|
|
StringLiteral("S_16BYTE_LITERALS")}, // 0x0E
|
|
|
|
{StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F
|
|
|
|
{StringLiteral("") /*FIXME??*/,
|
|
|
|
StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10
|
|
|
|
{StringLiteral("thread_local_regular"),
|
|
|
|
StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11
|
|
|
|
{StringLiteral("thread_local_zerofill"),
|
|
|
|
StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12
|
|
|
|
{StringLiteral("thread_local_variables"),
|
|
|
|
StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13
|
|
|
|
{StringLiteral("thread_local_variable_pointers"),
|
|
|
|
StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14
|
|
|
|
{StringLiteral("thread_local_init_function_pointers"),
|
|
|
|
StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15
|
2009-08-11 02:15:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// SectionAttrDescriptors - This is an array of descriptors for section
|
|
|
|
/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
|
2014-03-10 08:55:07 +08:00
|
|
|
/// by attribute, instead it is searched.
|
2017-01-31 02:46:26 +08:00
|
|
|
static constexpr struct {
|
2009-08-11 02:15:01 +08:00
|
|
|
unsigned AttrFlag;
|
2017-01-31 02:46:26 +08:00
|
|
|
StringLiteral AssemblerName, EnumName;
|
2009-08-11 02:15:01 +08:00
|
|
|
} SectionAttrDescriptors[] = {
|
|
|
|
#define ENTRY(ASMNAME, ENUM) \
|
2017-01-31 03:05:09 +08:00
|
|
|
{ MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
|
2009-08-11 02:15:01 +08:00
|
|
|
ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
|
|
|
|
ENTRY("no_toc", S_ATTR_NO_TOC)
|
|
|
|
ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
|
|
|
|
ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
|
|
|
|
ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
|
|
|
|
ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
|
|
|
|
ENTRY("debug", S_ATTR_DEBUG)
|
2017-01-31 02:46:26 +08:00
|
|
|
ENTRY("" /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
|
|
|
|
ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC)
|
|
|
|
ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
|
2009-08-11 02:15:01 +08:00
|
|
|
#undef ENTRY
|
2017-01-31 03:05:09 +08:00
|
|
|
{ 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size
|
2009-08-11 02:15:01 +08:00
|
|
|
};
|
|
|
|
|
2010-04-09 05:26:26 +08:00
|
|
|
MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
|
2015-03-11 06:00:25 +08:00
|
|
|
unsigned TAA, unsigned reserved2, SectionKind K,
|
|
|
|
MCSymbol *Begin)
|
2020-04-16 06:49:05 +08:00
|
|
|
: MCSection(SV_MachO, Section, K, Begin), TypeAndAttributes(TAA),
|
2015-03-11 06:00:25 +08:00
|
|
|
Reserved2(reserved2) {
|
2010-04-09 05:26:26 +08:00
|
|
|
assert(Segment.size() <= 16 && Section.size() <= 16 &&
|
|
|
|
"Segment or section string too long");
|
|
|
|
for (unsigned i = 0; i != 16; ++i) {
|
|
|
|
if (i < Segment.size())
|
|
|
|
SegmentName[i] = Segment[i];
|
|
|
|
else
|
|
|
|
SegmentName[i] = 0;
|
2010-10-22 06:04:05 +08:00
|
|
|
}
|
2009-08-11 02:15:01 +08:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:07:00 +08:00
|
|
|
void MCSectionMachO::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
|
2013-04-18 05:18:16 +08:00
|
|
|
raw_ostream &OS,
|
|
|
|
const MCExpr *Subsection) const {
|
2020-04-16 06:49:05 +08:00
|
|
|
OS << "\t.section\t" << getSegmentName() << ',' << getName();
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// Get the section type and attributes.
|
|
|
|
unsigned TAA = getTypeAndAttributes();
|
|
|
|
if (TAA == 0) {
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:49:54 +08:00
|
|
|
MachO::SectionType SectionType = getType();
|
2014-03-07 15:36:05 +08:00
|
|
|
assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
|
2009-08-11 02:15:01 +08:00
|
|
|
"Invalid SectionType specified!");
|
|
|
|
|
2016-10-05 09:02:34 +08:00
|
|
|
if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
|
2011-02-22 01:27:17 +08:00
|
|
|
OS << ',';
|
2009-08-11 02:15:01 +08:00
|
|
|
OS << SectionTypeDescriptors[SectionType].AssemblerName;
|
2011-02-22 05:07:07 +08:00
|
|
|
} else {
|
2011-02-22 01:27:17 +08:00
|
|
|
// If we have no name for the attribute, stop here.
|
2011-02-22 05:07:07 +08:00
|
|
|
OS << '\n';
|
2011-02-22 01:27:17 +08:00
|
|
|
return;
|
2011-02-22 05:07:07 +08:00
|
|
|
}
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// If we don't have any attributes, we're done.
|
2014-03-07 15:36:05 +08:00
|
|
|
unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
|
2009-08-11 02:15:01 +08:00
|
|
|
if (SectionAttrs == 0) {
|
|
|
|
// If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as
|
|
|
|
// the attribute specifier.
|
|
|
|
if (Reserved2 != 0)
|
|
|
|
OS << ",none," << Reserved2;
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check each attribute to see if we have it.
|
|
|
|
char Separator = ',';
|
2011-02-22 01:27:17 +08:00
|
|
|
for (unsigned i = 0;
|
|
|
|
SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
|
|
|
|
++i) {
|
2009-08-11 02:15:01 +08:00
|
|
|
// Check to see if we have this attribute.
|
|
|
|
if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
|
|
|
|
continue;
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// Yep, clear it and print it.
|
|
|
|
SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
OS << Separator;
|
2016-10-05 09:02:34 +08:00
|
|
|
if (!SectionAttrDescriptors[i].AssemblerName.empty())
|
2009-08-11 02:15:01 +08:00
|
|
|
OS << SectionAttrDescriptors[i].AssemblerName;
|
|
|
|
else
|
|
|
|
OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
|
|
|
|
Separator = '+';
|
|
|
|
}
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
assert(SectionAttrs == 0 && "Unknown section attributes!");
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// If we have a S_SYMBOL_STUBS size specified, print it.
|
|
|
|
if (Reserved2 != 0)
|
|
|
|
OS << ',' << Reserved2;
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:07:00 +08:00
|
|
|
bool MCSectionMachO::useCodeAlign() const {
|
2014-03-07 15:36:05 +08:00
|
|
|
return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
|
2010-10-05 01:32:41 +08:00
|
|
|
}
|
|
|
|
|
2010-11-18 04:03:54 +08:00
|
|
|
bool MCSectionMachO::isVirtualSection() const {
|
2014-03-07 15:36:05 +08:00
|
|
|
return (getType() == MachO::S_ZEROFILL ||
|
|
|
|
getType() == MachO::S_GB_ZEROFILL ||
|
|
|
|
getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
|
2010-11-18 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
|
|
|
|
/// This is a string that can appear after a .section directive in a mach-o
|
|
|
|
/// flavored .s file. If successful, this fills in the specified Out
|
|
|
|
/// parameters and returns an empty string. When an invalid section
|
|
|
|
/// specifier is present, this returns a string indicating the problem.
|
2021-02-24 00:01:27 +08:00
|
|
|
Error MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
|
|
|
|
StringRef &Segment, // Out.
|
|
|
|
StringRef &Section, // Out.
|
|
|
|
unsigned &TAA, // Out.
|
|
|
|
bool &TAAParsed, // Out.
|
|
|
|
unsigned &StubSize) { // Out.
|
2011-03-19 10:42:31 +08:00
|
|
|
TAAParsed = false;
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2014-03-10 08:55:07 +08:00
|
|
|
SmallVector<StringRef, 5> SplitSpec;
|
2015-09-10 14:12:31 +08:00
|
|
|
Spec.split(SplitSpec, ',');
|
2014-03-10 08:55:07 +08:00
|
|
|
// Remove leading and trailing whitespace.
|
|
|
|
auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
|
|
|
|
return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
|
|
|
|
};
|
|
|
|
Segment = GetEmptyOrTrim(0);
|
|
|
|
Section = GetEmptyOrTrim(1);
|
|
|
|
StringRef SectionType = GetEmptyOrTrim(2);
|
|
|
|
StringRef Attrs = GetEmptyOrTrim(3);
|
|
|
|
StringRef StubSizeStr = GetEmptyOrTrim(4);
|
2009-08-11 02:15:01 +08:00
|
|
|
|
2021-02-24 00:01:27 +08:00
|
|
|
// Verify that the section is present.
|
2014-03-10 08:55:07 +08:00
|
|
|
if (Section.empty())
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier requires a segment "
|
|
|
|
"and section separated by a comma");
|
2014-03-10 08:55:07 +08:00
|
|
|
|
2021-02-24 00:01:27 +08:00
|
|
|
// Verify that the section is not too long.
|
2014-03-10 08:55:07 +08:00
|
|
|
if (Section.size() > 16)
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier requires a section "
|
|
|
|
"whose length is between 1 and 16 characters");
|
2009-08-11 02:15:01 +08:00
|
|
|
|
|
|
|
// If there is no comma after the section, we're done.
|
2011-03-19 10:42:31 +08:00
|
|
|
TAA = 0;
|
2009-08-11 02:15:01 +08:00
|
|
|
StubSize = 0;
|
2014-03-10 08:55:07 +08:00
|
|
|
if (SectionType.empty())
|
2021-02-24 00:01:27 +08:00
|
|
|
return Error::success();
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// Figure out which section type it is.
|
2021-01-12 10:48:05 +08:00
|
|
|
auto TypeDescriptor =
|
|
|
|
llvm::find_if(SectionTypeDescriptors,
|
|
|
|
[&](decltype(*SectionTypeDescriptors) &Descriptor) {
|
|
|
|
return SectionType == Descriptor.AssemblerName;
|
|
|
|
});
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// If we didn't find the section type, reject it.
|
2014-03-10 08:55:07 +08:00
|
|
|
if (TypeDescriptor == std::end(SectionTypeDescriptors))
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier uses an unknown "
|
|
|
|
"section type");
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-08-11 02:15:01 +08:00
|
|
|
// Remember the TypeID.
|
2014-03-10 08:55:07 +08:00
|
|
|
TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
|
2011-03-19 10:42:31 +08:00
|
|
|
TAAParsed = true;
|
2009-08-11 02:15:01 +08:00
|
|
|
|
|
|
|
// If we have no comma after the section type, there are no attributes.
|
2014-03-10 08:55:07 +08:00
|
|
|
if (Attrs.empty()) {
|
2009-08-11 02:15:01 +08:00
|
|
|
// S_SYMBOL_STUBS always require a symbol stub size specifier.
|
2014-03-07 15:36:05 +08:00
|
|
|
if (TAA == MachO::S_SYMBOL_STUBS)
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier of type "
|
|
|
|
"'symbol_stubs' requires a size specifier");
|
|
|
|
return Error::success();
|
2009-08-11 02:15:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The attribute list is a '+' separated list of attributes.
|
2014-03-10 08:55:07 +08:00
|
|
|
SmallVector<StringRef, 1> SectionAttrs;
|
2015-09-10 14:12:31 +08:00
|
|
|
Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
2014-03-10 08:55:07 +08:00
|
|
|
|
|
|
|
for (StringRef &SectionAttr : SectionAttrs) {
|
2021-01-12 10:48:05 +08:00
|
|
|
auto AttrDescriptorI =
|
|
|
|
llvm::find_if(SectionAttrDescriptors,
|
|
|
|
[&](decltype(*SectionAttrDescriptors) &Descriptor) {
|
|
|
|
return SectionAttr.trim() == Descriptor.AssemblerName;
|
|
|
|
});
|
2014-03-10 08:55:07 +08:00
|
|
|
if (AttrDescriptorI == std::end(SectionAttrDescriptors))
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier has invalid "
|
|
|
|
"attribute");
|
2014-03-10 08:55:07 +08:00
|
|
|
|
|
|
|
TAA |= AttrDescriptorI->AttrFlag;
|
|
|
|
}
|
2009-08-11 02:15:01 +08:00
|
|
|
|
|
|
|
// Okay, we've parsed the section attributes, see if we have a stub size spec.
|
2014-03-10 08:55:07 +08:00
|
|
|
if (StubSizeStr.empty()) {
|
2009-08-11 02:15:01 +08:00
|
|
|
// S_SYMBOL_STUBS always require a symbol stub size specifier.
|
2014-03-07 15:36:05 +08:00
|
|
|
if (TAA == MachO::S_SYMBOL_STUBS)
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier of type "
|
|
|
|
"'symbol_stubs' requires a size specifier");
|
|
|
|
return Error::success();
|
2009-08-11 02:15:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS.
|
2014-03-07 15:36:05 +08:00
|
|
|
if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier cannot have a stub "
|
|
|
|
"size specified because it does not have type "
|
|
|
|
"'symbol_stubs'");
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2009-09-20 14:58:54 +08:00
|
|
|
// Convert the stub size from a string to an integer.
|
|
|
|
if (StubSizeStr.getAsInteger(0, StubSize))
|
2021-02-24 00:01:27 +08:00
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
"mach-o section specifier has a malformed "
|
|
|
|
"stub size");
|
2010-10-22 06:04:05 +08:00
|
|
|
|
2021-02-24 00:01:27 +08:00
|
|
|
return Error::success();
|
2009-08-11 02:15:01 +08:00
|
|
|
}
|