X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FMC%2FMCSectionMachO.cpp;h=46beda4d9425189ab631ba7ece47e789395b0aac;hb=29b31c6eafb2dc4b07d092d1c0809b00a605da3b;hp=29829463c18ff18cc09c015685979e52fc0e0a2e;hpb=f5955c7dacd95d503c8287b04f28d7a7b6059cbb;p=oota-llvm.git diff --git a/lib/MC/MCSectionMachO.cpp b/lib/MC/MCSectionMachO.cpp index 29829463c18..46beda4d942 100644 --- a/lib/MC/MCSectionMachO.cpp +++ b/lib/MC/MCSectionMachO.cpp @@ -20,7 +20,7 @@ static const struct { const char *AssemblerName, *EnumName; } SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE+1] = { { "regular", "S_REGULAR" }, // 0x00 - { 0, "S_ZEROFILL" }, // 0x01 + { nullptr, "S_ZEROFILL" }, // 0x01 { "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02 { "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03 { "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04 @@ -31,11 +31,11 @@ static const struct { { "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09 { "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A { "coalesced", "S_COALESCED" }, // 0x0B - { 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C + { nullptr, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C { "interposing", "S_INTERPOSING" }, // 0x0D { "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E - { 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F - { 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10 + { nullptr, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F + { nullptr, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10 { "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11 { "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12 { "thread_local_variables", "S_THREAD_LOCAL_VARIABLES" }, // 0x13 @@ -48,8 +48,7 @@ static const struct { /// SectionAttrDescriptors - This is an array of descriptors for section /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed -/// by attribute, instead it is searched. The last entry has an AttrFlagEnd -/// AttrFlag value. +/// by attribute, instead it is searched. static const struct { unsigned AttrFlag; const char *AssemblerName, *EnumName; @@ -63,13 +62,11 @@ 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 /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS) -ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC) -ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC) +ENTRY(nullptr /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS) +ENTRY(nullptr /*FIXME*/, S_ATTR_EXT_RELOC) +ENTRY(nullptr /*FIXME*/, S_ATTR_LOC_RELOC) #undef ENTRY - { 0, "none", 0 }, // used if section has no attributes but has a stub size -#define AttrFlagEnd 0xffffffff // non-legal value, multiple attribute bits set - { AttrFlagEnd, 0, 0 } + { 0, "none", nullptr }, // used if section has no attributes but has a stub size }; MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section, @@ -164,14 +161,6 @@ bool MCSectionMachO::isVirtualSection() const { getType() == MachO::S_THREAD_LOCAL_ZEROFILL); } -/// StripSpaces - This removes leading and trailing spaces from the StringRef. -static void StripSpaces(StringRef &Str) { - while (!Str.empty() && isspace(static_cast(Str[0]))) - Str = Str.substr(1); - while (!Str.empty() && isspace(static_cast(Str.back()))) - Str = Str.substr(0, Str.size()-1); -} - /// 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 @@ -184,65 +173,57 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. bool &TAAParsed, // Out. unsigned &StubSize) { // Out. TAAParsed = false; - // Find the first comma. - std::pair Comma = Spec.split(','); - - // If there is no comma, we fail. - if (Comma.second.empty()) - return "mach-o section specifier requires a segment and section " - "separated by a comma"; - // Capture segment, remove leading and trailing whitespace. - Segment = Comma.first; - StripSpaces(Segment); + SmallVector SplitSpec; + Spec.split(SplitSpec, ","); + // 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); // Verify that the segment is present and not too long. if (Segment.empty() || Segment.size() > 16) return "mach-o section specifier requires a segment whose length is " "between 1 and 16 characters"; - // Split the section name off from any attributes if present. - Comma = Comma.second.split(','); - - // Capture section, remove leading and trailing whitespace. - Section = Comma.first; - StripSpaces(Section); - // Verify that the section is present and not too long. - if (Section.empty() || Section.size() > 16) + if (Section.empty()) + return "mach-o section specifier requires a segment and section " + "separated by a comma"; + + if (Section.size() > 16) return "mach-o section specifier requires a section whose length is " "between 1 and 16 characters"; // If there is no comma after the section, we're done. TAA = 0; StubSize = 0; - if (Comma.second.empty()) + if (SectionType.empty()) return ""; - // Otherwise, we need to parse the section type and attributes. - Comma = Comma.second.split(','); - - // Get the section type. - StringRef SectionType = Comma.first; - StripSpaces(SectionType); - // Figure out which section type it is. - unsigned TypeID; - for (TypeID = 0; TypeID != MachO::LAST_KNOWN_SECTION_TYPE + 1; ++TypeID) - if (SectionTypeDescriptors[TypeID].AssemblerName && - SectionType == SectionTypeDescriptors[TypeID].AssemblerName) - break; + auto TypeDescriptor = std::find_if( + std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors), + [&](decltype(*SectionTypeDescriptors) &Descriptor) { + return Descriptor.AssemblerName && + SectionType == Descriptor.AssemblerName; + }); // If we didn't find the section type, reject it. - if (TypeID > MachO::LAST_KNOWN_SECTION_TYPE) + if (TypeDescriptor == std::end(SectionTypeDescriptors)) return "mach-o section specifier uses an unknown section type"; // Remember the TypeID. - TAA = TypeID; + TAA = TypeDescriptor - std::begin(SectionTypeDescriptors); TAAParsed = true; // If we have no comma after the section type, there are no attributes. - if (Comma.second.empty()) { + if (Attrs.empty()) { // S_SYMBOL_STUBS always require a symbol stub size specifier. if (TAA == MachO::S_SYMBOL_STUBS) return "mach-o section specifier of type 'symbol_stubs' requires a size " @@ -250,36 +231,25 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. return ""; } - // Otherwise, we do have some attributes. Split off the size specifier if - // present. - Comma = Comma.second.split(','); - StringRef Attrs = Comma.first; - // The attribute list is a '+' separated list of attributes. - std::pair Plus = Attrs.split('+'); - - while (1) { - StringRef Attr = Plus.first; - StripSpaces(Attr); - - // Look up the attribute. - 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('+'); - }; + SmallVector SectionAttrs; + Attrs.split(SectionAttrs, "+", /*MaxSplit=*/-1, /*KeepEmpty=*/false); + + for (StringRef &SectionAttr : SectionAttrs) { + auto AttrDescriptorI = std::find_if( + std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors), + [&](decltype(*SectionAttrDescriptors) &Descriptor) { + return Descriptor.AssemblerName && + SectionAttr.trim() == Descriptor.AssemblerName; + }); + if (AttrDescriptorI == std::end(SectionAttrDescriptors)) + return "mach-o section specifier has invalid attribute"; + + TAA |= AttrDescriptorI->AttrFlag; + } // Okay, we've parsed the section attributes, see if we have a stub size spec. - if (Comma.second.empty()) { + if (StubSizeStr.empty()) { // S_SYMBOL_STUBS always require a symbol stub size specifier. if (TAA == MachO::S_SYMBOL_STUBS) return "mach-o section specifier of type 'symbol_stubs' requires a size " @@ -292,10 +262,6 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. return "mach-o section specifier cannot have a stub size specified because " "it does not have type 'symbol_stubs'"; - // Okay, if we do, it must be a number. - StringRef StubSizeStr = Comma.second; - StripSpaces(StubSizeStr); - // Convert the stub size from a string to an integer. if (StubSizeStr.getAsInteger(0, StubSize)) return "mach-o section specifier has a malformed stub size";