Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCSectionMachO.cpp - MachO Code Section Representation ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/MC/MCSectionMachO.h" |
| 10 | #include "llvm/MC/MCContext.h" |
| 11 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 12 | #include <cctype> |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | |
| 15 | /// SectionTypeDescriptors - These are strings that describe the various section |
| 16 | /// types. This *must* be kept in order with and stay synchronized with the |
| 17 | /// section type list. |
Benjamin Kramer | a846e0b | 2017-01-30 18:46:26 +0000 | [diff] [blame] | 18 | static constexpr struct { |
| 19 | StringLiteral AssemblerName, EnumName; |
Benjamin Kramer | a9df941 | 2017-01-30 19:05:09 +0000 | [diff] [blame] | 20 | } SectionTypeDescriptors[MachO::LAST_KNOWN_SECTION_TYPE + 1] = { |
| 21 | {StringLiteral("regular"), StringLiteral("S_REGULAR")}, // 0x00 |
| 22 | {StringLiteral(""), StringLiteral("S_ZEROFILL")}, // 0x01 |
| 23 | {StringLiteral("cstring_literals"), |
| 24 | StringLiteral("S_CSTRING_LITERALS")}, // 0x02 |
| 25 | {StringLiteral("4byte_literals"), |
| 26 | StringLiteral("S_4BYTE_LITERALS")}, // 0x03 |
| 27 | {StringLiteral("8byte_literals"), |
| 28 | StringLiteral("S_8BYTE_LITERALS")}, // 0x04 |
| 29 | {StringLiteral("literal_pointers"), |
| 30 | StringLiteral("S_LITERAL_POINTERS")}, // 0x05 |
| 31 | {StringLiteral("non_lazy_symbol_pointers"), |
| 32 | StringLiteral("S_NON_LAZY_SYMBOL_POINTERS")}, // 0x06 |
| 33 | {StringLiteral("lazy_symbol_pointers"), |
| 34 | StringLiteral("S_LAZY_SYMBOL_POINTERS")}, // 0x07 |
| 35 | {StringLiteral("symbol_stubs"), StringLiteral("S_SYMBOL_STUBS")}, // 0x08 |
| 36 | {StringLiteral("mod_init_funcs"), |
| 37 | StringLiteral("S_MOD_INIT_FUNC_POINTERS")}, // 0x09 |
| 38 | {StringLiteral("mod_term_funcs"), |
| 39 | StringLiteral("S_MOD_TERM_FUNC_POINTERS")}, // 0x0A |
| 40 | {StringLiteral("coalesced"), StringLiteral("S_COALESCED")}, // 0x0B |
| 41 | {StringLiteral("") /*FIXME??*/, StringLiteral("S_GB_ZEROFILL")}, // 0x0C |
| 42 | {StringLiteral("interposing"), StringLiteral("S_INTERPOSING")}, // 0x0D |
| 43 | {StringLiteral("16byte_literals"), |
| 44 | StringLiteral("S_16BYTE_LITERALS")}, // 0x0E |
| 45 | {StringLiteral("") /*FIXME??*/, StringLiteral("S_DTRACE_DOF")}, // 0x0F |
| 46 | {StringLiteral("") /*FIXME??*/, |
| 47 | StringLiteral("S_LAZY_DYLIB_SYMBOL_POINTERS")}, // 0x10 |
| 48 | {StringLiteral("thread_local_regular"), |
| 49 | StringLiteral("S_THREAD_LOCAL_REGULAR")}, // 0x11 |
| 50 | {StringLiteral("thread_local_zerofill"), |
| 51 | StringLiteral("S_THREAD_LOCAL_ZEROFILL")}, // 0x12 |
| 52 | {StringLiteral("thread_local_variables"), |
| 53 | StringLiteral("S_THREAD_LOCAL_VARIABLES")}, // 0x13 |
| 54 | {StringLiteral("thread_local_variable_pointers"), |
| 55 | StringLiteral("S_THREAD_LOCAL_VARIABLE_POINTERS")}, // 0x14 |
| 56 | {StringLiteral("thread_local_init_function_pointers"), |
| 57 | StringLiteral("S_THREAD_LOCAL_INIT_FUNCTION_POINTERS")}, // 0x15 |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 60 | /// SectionAttrDescriptors - This is an array of descriptors for section |
| 61 | /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 62 | /// by attribute, instead it is searched. |
Benjamin Kramer | a846e0b | 2017-01-30 18:46:26 +0000 | [diff] [blame] | 63 | static constexpr struct { |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 64 | unsigned AttrFlag; |
Benjamin Kramer | a846e0b | 2017-01-30 18:46:26 +0000 | [diff] [blame] | 65 | StringLiteral AssemblerName, EnumName; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 66 | } SectionAttrDescriptors[] = { |
| 67 | #define ENTRY(ASMNAME, ENUM) \ |
Benjamin Kramer | a9df941 | 2017-01-30 19:05:09 +0000 | [diff] [blame] | 68 | { MachO::ENUM, StringLiteral(ASMNAME), StringLiteral(#ENUM) }, |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 69 | ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS) |
| 70 | ENTRY("no_toc", S_ATTR_NO_TOC) |
| 71 | ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS) |
| 72 | ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP) |
| 73 | ENTRY("live_support", S_ATTR_LIVE_SUPPORT) |
| 74 | ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE) |
| 75 | ENTRY("debug", S_ATTR_DEBUG) |
Benjamin Kramer | a846e0b | 2017-01-30 18:46:26 +0000 | [diff] [blame] | 76 | ENTRY("" /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS) |
| 77 | ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC) |
| 78 | ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 79 | #undef ENTRY |
Benjamin Kramer | a9df941 | 2017-01-30 19:05:09 +0000 | [diff] [blame] | 80 | { 0, StringLiteral("none"), StringLiteral("") }, // used if section has no attributes but has a stub size |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 83 | MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section, |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 84 | unsigned TAA, unsigned reserved2, SectionKind K, |
| 85 | MCSymbol *Begin) |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 86 | : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 87 | Reserved2(reserved2) { |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 88 | assert(Segment.size() <= 16 && Section.size() <= 16 && |
| 89 | "Segment or section string too long"); |
| 90 | for (unsigned i = 0; i != 16; ++i) { |
| 91 | if (i < Segment.size()) |
| 92 | SegmentName[i] = Segment[i]; |
| 93 | else |
| 94 | SegmentName[i] = 0; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 96 | if (i < Section.size()) |
| 97 | SectionName[i] = Section[i]; |
| 98 | else |
| 99 | SectionName[i] = 0; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Rafael Espindola | e0eba3c | 2017-01-30 15:38:43 +0000 | [diff] [blame] | 103 | void MCSectionMachO::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 104 | raw_ostream &OS, |
| 105 | const MCExpr *Subsection) const { |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 106 | OS << "\t.section\t" << getSegmentName() << ',' << getSectionName(); |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 108 | // Get the section type and attributes. |
| 109 | unsigned TAA = getTypeAndAttributes(); |
| 110 | if (TAA == 0) { |
| 111 | OS << '\n'; |
| 112 | return; |
| 113 | } |
| 114 | |
David Majnemer | cd481d3 | 2014-03-07 18:49:54 +0000 | [diff] [blame] | 115 | MachO::SectionType SectionType = getType(); |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 116 | assert(SectionType <= MachO::LAST_KNOWN_SECTION_TYPE && |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 117 | "Invalid SectionType specified!"); |
| 118 | |
Mehdi Amini | a28bb09 | 2016-10-05 01:02:34 +0000 | [diff] [blame] | 119 | if (!SectionTypeDescriptors[SectionType].AssemblerName.empty()) { |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 120 | OS << ','; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 121 | OS << SectionTypeDescriptors[SectionType].AssemblerName; |
Stuart Hastings | d7927e0 | 2011-02-21 21:07:07 +0000 | [diff] [blame] | 122 | } else { |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 123 | // If we have no name for the attribute, stop here. |
Stuart Hastings | d7927e0 | 2011-02-21 21:07:07 +0000 | [diff] [blame] | 124 | OS << '\n'; |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 125 | return; |
Stuart Hastings | d7927e0 | 2011-02-21 21:07:07 +0000 | [diff] [blame] | 126 | } |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 128 | // If we don't have any attributes, we're done. |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 129 | unsigned SectionAttrs = TAA & MachO::SECTION_ATTRIBUTES; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 130 | if (SectionAttrs == 0) { |
| 131 | // If we have a S_SYMBOL_STUBS size specified, print it along with 'none' as |
| 132 | // the attribute specifier. |
| 133 | if (Reserved2 != 0) |
| 134 | OS << ",none," << Reserved2; |
| 135 | OS << '\n'; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Check each attribute to see if we have it. |
| 140 | char Separator = ','; |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 141 | for (unsigned i = 0; |
| 142 | SectionAttrs != 0 && SectionAttrDescriptors[i].AttrFlag; |
| 143 | ++i) { |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 144 | // Check to see if we have this attribute. |
| 145 | if ((SectionAttrDescriptors[i].AttrFlag & SectionAttrs) == 0) |
| 146 | continue; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 148 | // Yep, clear it and print it. |
| 149 | SectionAttrs &= ~SectionAttrDescriptors[i].AttrFlag; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 151 | OS << Separator; |
Mehdi Amini | a28bb09 | 2016-10-05 01:02:34 +0000 | [diff] [blame] | 152 | if (!SectionAttrDescriptors[i].AssemblerName.empty()) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 153 | OS << SectionAttrDescriptors[i].AssemblerName; |
| 154 | else |
| 155 | OS << "<<" << SectionAttrDescriptors[i].EnumName << ">>"; |
| 156 | Separator = '+'; |
| 157 | } |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 159 | assert(SectionAttrs == 0 && "Unknown section attributes!"); |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 161 | // If we have a S_SYMBOL_STUBS size specified, print it. |
| 162 | if (Reserved2 != 0) |
| 163 | OS << ',' << Reserved2; |
| 164 | OS << '\n'; |
| 165 | } |
| 166 | |
Jan Wen Voung | 87f77b5 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 167 | bool MCSectionMachO::UseCodeAlign() const { |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 168 | return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS); |
Jan Wen Voung | 87f77b5 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 171 | bool MCSectionMachO::isVirtualSection() const { |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 172 | return (getType() == MachO::S_ZEROFILL || |
| 173 | getType() == MachO::S_GB_ZEROFILL || |
| 174 | getType() == MachO::S_THREAD_LOCAL_ZEROFILL); |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 177 | /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec". |
| 178 | /// This is a string that can appear after a .section directive in a mach-o |
| 179 | /// flavored .s file. If successful, this fills in the specified Out |
| 180 | /// parameters and returns an empty string. When an invalid section |
| 181 | /// specifier is present, this returns a string indicating the problem. |
| 182 | std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. |
| 183 | StringRef &Segment, // Out. |
| 184 | StringRef &Section, // Out. |
| 185 | unsigned &TAA, // Out. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 186 | bool &TAAParsed, // Out. |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 187 | unsigned &StubSize) { // Out. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 188 | TAAParsed = false; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 189 | |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 190 | SmallVector<StringRef, 5> SplitSpec; |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 191 | Spec.split(SplitSpec, ','); |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 192 | // Remove leading and trailing whitespace. |
| 193 | auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef { |
| 194 | return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef(); |
| 195 | }; |
| 196 | Segment = GetEmptyOrTrim(0); |
| 197 | Section = GetEmptyOrTrim(1); |
| 198 | StringRef SectionType = GetEmptyOrTrim(2); |
| 199 | StringRef Attrs = GetEmptyOrTrim(3); |
| 200 | StringRef StubSizeStr = GetEmptyOrTrim(4); |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 201 | |
| 202 | // Verify that the segment is present and not too long. |
| 203 | if (Segment.empty() || Segment.size() > 16) |
| 204 | return "mach-o section specifier requires a segment whose length is " |
| 205 | "between 1 and 16 characters"; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 207 | // Verify that the section is present and not too long. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 208 | if (Section.empty()) |
| 209 | return "mach-o section specifier requires a segment and section " |
| 210 | "separated by a comma"; |
| 211 | |
| 212 | if (Section.size() > 16) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 213 | return "mach-o section specifier requires a section whose length is " |
| 214 | "between 1 and 16 characters"; |
| 215 | |
| 216 | // If there is no comma after the section, we're done. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 217 | TAA = 0; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 218 | StubSize = 0; |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 219 | if (SectionType.empty()) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 220 | return ""; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 222 | // Figure out which section type it is. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 223 | auto TypeDescriptor = std::find_if( |
| 224 | std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors), |
David Majnemer | 596587f | 2014-03-10 01:04:18 +0000 | [diff] [blame] | 225 | [&](decltype(*SectionTypeDescriptors) &Descriptor) { |
Mehdi Amini | a28bb09 | 2016-10-05 01:02:34 +0000 | [diff] [blame] | 226 | return SectionType == Descriptor.AssemblerName; |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 227 | }); |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 229 | // If we didn't find the section type, reject it. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 230 | if (TypeDescriptor == std::end(SectionTypeDescriptors)) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 231 | return "mach-o section specifier uses an unknown section type"; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 233 | // Remember the TypeID. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 234 | TAA = TypeDescriptor - std::begin(SectionTypeDescriptors); |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 235 | TAAParsed = true; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 236 | |
| 237 | // If we have no comma after the section type, there are no attributes. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 238 | if (Attrs.empty()) { |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 239 | // S_SYMBOL_STUBS always require a symbol stub size specifier. |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 240 | if (TAA == MachO::S_SYMBOL_STUBS) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 241 | return "mach-o section specifier of type 'symbol_stubs' requires a size " |
| 242 | "specifier"; |
| 243 | return ""; |
| 244 | } |
| 245 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 246 | // The attribute list is a '+' separated list of attributes. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 247 | SmallVector<StringRef, 1> SectionAttrs; |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 248 | Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 249 | |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 250 | for (StringRef &SectionAttr : SectionAttrs) { |
| 251 | auto AttrDescriptorI = std::find_if( |
| 252 | std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors), |
David Majnemer | 596587f | 2014-03-10 01:04:18 +0000 | [diff] [blame] | 253 | [&](decltype(*SectionAttrDescriptors) &Descriptor) { |
Mehdi Amini | a28bb09 | 2016-10-05 01:02:34 +0000 | [diff] [blame] | 254 | return SectionAttr.trim() == Descriptor.AssemblerName; |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 255 | }); |
| 256 | if (AttrDescriptorI == std::end(SectionAttrDescriptors)) |
| 257 | return "mach-o section specifier has invalid attribute"; |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 258 | |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 259 | TAA |= AttrDescriptorI->AttrFlag; |
| 260 | } |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 261 | |
| 262 | // Okay, we've parsed the section attributes, see if we have a stub size spec. |
David Majnemer | e500ec1 | 2014-03-10 00:55:07 +0000 | [diff] [blame] | 263 | if (StubSizeStr.empty()) { |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 264 | // S_SYMBOL_STUBS always require a symbol stub size specifier. |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 265 | if (TAA == MachO::S_SYMBOL_STUBS) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 266 | return "mach-o section specifier of type 'symbol_stubs' requires a size " |
| 267 | "specifier"; |
| 268 | return ""; |
| 269 | } |
| 270 | |
| 271 | // If we have a stub size spec, we must have a sectiontype of S_SYMBOL_STUBS. |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 272 | if ((TAA & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 273 | return "mach-o section specifier cannot have a stub size specified because " |
| 274 | "it does not have type 'symbol_stubs'"; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 275 | |
Chris Lattner | a15f004 | 2009-09-20 06:58:54 +0000 | [diff] [blame] | 276 | // Convert the stub size from a string to an integer. |
| 277 | if (StubSizeStr.getAsInteger(0, StubSize)) |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 278 | return "mach-o section specifier has a malformed stub size"; |
Jim Grosbach | 90600d3 | 2010-10-21 22:04:05 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 6c20391 | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 280 | return ""; |
| 281 | } |