#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/SectionKind.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
class MCAsmInfo;
class MCExpr;
class MCSymbol;
class Triple;
}
using namespace llvm;
static constexpr struct {
StringLiteral AssemblerName, EnumName;
} SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = {
{StringLiteral("regular"), StringLiteral("S_REGULAR")}, {StringLiteral("zerofill"), StringLiteral("S_ZEROFILL")}, {StringLiteral("cstring_literals"),
StringLiteral("S_CSTRING_LITERALS")}, {StringLiteral("4byte_literals"),
StringLiteral("S_4BYTE_LITERALS")}, {StringLiteral("8byte_literals"),
StringLiteral("S_8BYTE_LITERALS")}, {StringLiteral("literal_pointers"),
StringLiteral("S_LITERAL_POINTERS")}, {StringLiteral("non_lazy_symbol_pointers"),
StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, {StringLiteral("lazy_symbol_pointers"),
StringLiteral("S_LAZY_SYMBOL_POINTERS")}, {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, {StringLiteral("mod_init_funcs"),
StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, {StringLiteral("mod_term_funcs"),
StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, {StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, {StringLiteral("") , StringLiteral("S_GB_ZEROFILL")}, {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, {StringLiteral("16byte_literals"),
StringLiteral("S_16BYTE_LITERALS")}, {StringLiteral("") , StringLiteral("S_DTRACE_DOF")}, {StringLiteral("") ,
StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, {StringLiteral("thread_local_regular"),
StringLiteral("S_THREAD_LOCAL_REGULAR")}, {StringLiteral("thread_local_zerofill"),
StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, {StringLiteral("thread_local_variables"),
StringLiteral("S_THREAD_LOCAL_VARIABLES")}, {StringLiteral("thread_local_variable_pointers"),
StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, {StringLiteral("thread_local_init_function_pointers"),
StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, };
static constexpr struct {
unsigned AttrFlag;
StringLiteral AssemblerName, EnumName;
} SectionAttrDescriptors[] = {
#define ENTRY(ASMNAME, ENUM) \
{ MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) },
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)
ENTRY("" , S_ATTR_SOME_INSTRUCTIONS)
ENTRY("" , S_ATTR_EXT_RELOC)
ENTRY("" , S_ATTR_LOC_RELOC)
#undef ENTRY
{ 0, StringLiteral("none"), StringLiteral("") }, };
MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
unsigned TAA, unsigned reserved2, SectionKind K,
MCSymbol *Begin)
: MCSection(SV_MachO, Section, K, Begin), TypeAndAttributes(TAA),
Reserved2(reserved2) {
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;
}
}
void MCSectionMachO::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
raw_ostream &OS,
const MCExpr *Subsection) const {
OS << "\t.section\t" << getSegmentName() << ',' << getName();
unsigned TAA = getTypeAndAttributes();
if (TAA == 0) {
OS << '\n';
return;
}
MachO::SectionType SectionType = getType();
assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE &&
"Invalid SectionType specified!");
if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) {
OS << ',';
OS << SectionTypeDescriptors[SectionType].AssemblerName;
} else {
OS << '\n';
return;
}
unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES;
if (SectionAttrs == 0) {
if (Reserved2 != 0)
OS << ",none," << Reserved2;
OS << '\n';
return;
}
char Separator = ',';
for (unsigned i = 0;
SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag;
++i) {
if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
continue;
SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
OS << Separator;
if (!SectionAttrDescriptors[i].AssemblerName.empty())
OS << SectionAttrDescriptors[i].AssemblerName;
else
OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
Separator = '+';
}
assert(SectionAttrs == 0 && "Unknown section attributes!");
if (Reserved2 != 0)
OS << ',' << Reserved2;
OS << '\n';
}
bool MCSectionMachO::useCodeAlign() const {
return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
}
bool MCSectionMachO::isVirtualSection() const {
return (getType() == MachO::S_ZEROFILL ||
getType() == MachO::S_GB_ZEROFILL ||
getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
}
Error MCSectionMachO::ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize) { TAAParsed = false;
SmallVector<StringRef, 5> SplitSpec;
Spec.split(SplitSpec, ',');
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);
if (Section.empty())
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier requires a segment "
"and section separated by a comma");
if (Section.size() > 16)
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier requires a section "
"whose length is between 1 and 16 characters");
TAA = 0;
StubSize = 0;
if (SectionType.empty())
return Error::success();
auto TypeDescriptor =
llvm::find_if(SectionTypeDescriptors,
[&](decltype(*SectionTypeDescriptors) &Descriptor) {
return SectionType == Descriptor.AssemblerName;
});
if (TypeDescriptor == std::end(SectionTypeDescriptors))
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier uses an unknown "
"section type");
TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
TAAParsed = true;
if (Attrs.empty()) {
if (TAA == MachO::S_SYMBOL_STUBS)
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier of type "
"'symbol_stubs' requires a size specifier");
return Error::success();
}
SmallVector<StringRef, 1> SectionAttrs;
Attrs.split(SectionAttrs, '+', -1, false);
for (StringRef &SectionAttr : SectionAttrs) {
auto AttrDescriptorI =
llvm::find_if(SectionAttrDescriptors,
[&](decltype(*SectionAttrDescriptors) &Descriptor) {
return SectionAttr.trim() == Descriptor.AssemblerName;
});
if (AttrDescriptorI == std::end(SectionAttrDescriptors))
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier has invalid "
"attribute");
TAA |= AttrDescriptorI->AttrFlag;
}
if (StubSizeStr.empty()) {
if (TAA == MachO::S_SYMBOL_STUBS)
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier of type "
"'symbol_stubs' requires a size specifier");
return Error::success();
}
if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier cannot have a stub "
"size specified because it does not have type "
"'symbol_stubs'");
if (StubSizeStr.getAsInteger(0, StubSize))
return createStringError(inconvertibleErrorCode(),
"mach-o section specifier has a malformed "
"stub size");
return Error::success();
}