MCSectionMachO.cpp [plain text]
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
static const struct {
const char *AssemblerName, *EnumName;
} SectionTypeDescriptors[MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1] = {
{ "regular", "S_REGULAR" }, { 0, "S_ZEROFILL" }, { "cstring_literals", "S_CSTRING_LITERALS" }, { "4byte_literals", "S_4BYTE_LITERALS" }, { "8byte_literals", "S_8BYTE_LITERALS" }, { "literal_pointers", "S_LITERAL_POINTERS" }, { "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, { "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, { "symbol_stubs", "S_SYMBOL_STUBS" }, { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, { "coalesced", "S_COALESCED" }, { 0, "S_GB_ZEROFILL" }, { "interposing", "S_INTERPOSING" }, { "16byte_literals", "S_16BYTE_LITERALS" }, { 0, "S_DTRACE_DOF" }, { 0, "S_LAZY_DYLIB_SYMBOL_POINTERS" } };
static const struct {
unsigned AttrFlag;
const char *AssemblerName, *EnumName;
} SectionAttrDescriptors[] = {
#define ENTRY(ASMNAME, ENUM) \
{ MCSectionMachO::ENUM, ASMNAME, #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(0 , S_ATTR_SOME_INSTRUCTIONS)
ENTRY(0 , S_ATTR_EXT_RELOC)
ENTRY(0 , S_ATTR_LOC_RELOC)
#undef ENTRY
{ 0, "none", 0 }, #define AttrFlagEnd 0xffffffff // non legal value, multiple attribute bits set
{ AttrFlagEnd, 0, 0 }
};
MCSectionMachO *MCSectionMachO::
Create(StringRef Segment, StringRef Section,
unsigned TypeAndAttributes, unsigned Reserved2,
SectionKind K, MCContext &Ctx) {
return new (Ctx) MCSectionMachO(Segment, Section, TypeAndAttributes,
Reserved2, K);
}
void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI,
raw_ostream &OS) const {
OS << "\t.section\t" << getSegmentName() << ',' << getSectionName();
unsigned TAA = getTypeAndAttributes();
if (TAA == 0) {
OS << '\n';
return;
}
OS << ',';
unsigned SectionType = TAA & MCSectionMachO::SECTION_TYPE;
assert(SectionType <= MCSectionMachO::LAST_KNOWN_SECTION_TYPE &&
"Invalid SectionType specified!");
if (SectionTypeDescriptors[SectionType].AssemblerName)
OS << SectionTypeDescriptors[SectionType].AssemblerName;
else
OS << "<<" << SectionTypeDescriptors[SectionType].EnumName << ">>";
unsigned SectionAttrs = TAA & MCSectionMachO::SECTION_ATTRIBUTES;
if (SectionAttrs == 0) {
if (Reserved2 != 0)
OS << ",none," << Reserved2;
OS << '\n';
return;
}
char Separator = ',';
for (unsigned i = 0; SectionAttrDescriptors[i].AttrFlag; ++i) {
if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0)
continue;
SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag;
OS << Separator;
if (SectionAttrDescriptors[i].AssemblerName)
OS << SectionAttrDescriptors[i].AssemblerName;
else
OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>";
Separator = '+';
}
assert(SectionAttrs == 0 && "Unknown section attributes!");
if (Reserved2 != 0)
OS << ',' << Reserved2;
OS << '\n';
}
static void StripSpaces(StringRef &Str) {
while (!Str.empty() && isspace(Str[0]))
Str = Str.substr(1);
while (!Str.empty() && isspace(Str.back()))
Str = Str.substr(0, Str.size()-1);
}
std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, unsigned &StubSize) { std::pair<StringRef, StringRef> Comma = Spec.split(',');
if (Comma.second.empty())
return "mach-o section specifier requires a segment and section "
"separated by a comma";
Segment = Comma.first;
StripSpaces(Segment);
if (Segment.empty() || Segment.size() > 16)
return "mach-o section specifier requires a segment whose length is "
"between 1 and 16 characters";
Comma = Comma.second.split(',');
Section = Comma.first;
StripSpaces(Section);
if (Section.empty() || Section.size() > 16)
return "mach-o section specifier requires a section whose length is "
"between 1 and 16 characters";
TAA = 0;
StubSize = 0;
if (Comma.second.empty())
return "";
Comma = Comma.second.split(',');
StringRef SectionType = Comma.first;
StripSpaces(SectionType);
unsigned TypeID;
for (TypeID = 0; TypeID !=MCSectionMachO::LAST_KNOWN_SECTION_TYPE+1; ++TypeID)
if (SectionTypeDescriptors[TypeID].AssemblerName &&
SectionType == SectionTypeDescriptors[TypeID].AssemblerName)
break;
if (TypeID > MCSectionMachO::LAST_KNOWN_SECTION_TYPE)
return "mach-o section specifier uses an unknown section type";
TAA = TypeID;
if (Comma.second.empty()) {
if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
return "mach-o section specifier of type 'symbol_stubs' requires a size "
"specifier";
return "";
}
Comma = Comma.second.split(',');
StringRef Attrs = Comma.first;
std::pair<StringRef, StringRef> Plus = Attrs.split('+');
while (1) {
StringRef Attr = Plus.first;
StripSpaces(Attr);
for (unsigned i = 0; ; ++i) {
if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd)
return "mach-o section specifier has invalid attribute";
if (SectionAttrDescriptors[i].AssemblerName &&
Attr == SectionAttrDescriptors[i].AssemblerName) {
TAA |= SectionAttrDescriptors[i].AttrFlag;
break;
}
}
if (Plus.second.empty()) break;
Plus = Plus.second.split('+');
};
if (Comma.second.empty()) {
if (TAA == MCSectionMachO::S_SYMBOL_STUBS)
return "mach-o section specifier of type 'symbol_stubs' requires a size "
"specifier";
return "";
}
if ((TAA & MCSectionMachO::SECTION_TYPE) != MCSectionMachO::S_SYMBOL_STUBS)
return "mach-o section specifier cannot have a stub size specified because "
"it does not have type 'symbol_stubs'";
StringRef StubSizeStr = Comma.second;
StripSpaces(StubSizeStr);
if (StubSizeStr.getAsInteger(0, StubSize))
return "mach-o section specifier has a malformed stub size";
return "";
}