1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDirectives.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCParser/MCAsmLexer.h"
20 #include "llvm/MC/MCParser/MCAsmParser.h"
21 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/MachO.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/SMLoc.h"
31 #include "llvm/Support/SourceMgr.h"
36 #include <system_error>
43 /// \brief Implementation of directive handling which is shared across all
45 class DarwinAsmParser : public MCAsmParserExtension {
46 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
47 void addDirectiveHandler(StringRef Directive) {
48 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
49 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
50 getParser().addDirectiveHandler(Directive, Handler);
53 bool parseSectionSwitch(StringRef Segment, StringRef Section,
54 unsigned TAA = 0, unsigned ImplicitAlign = 0,
55 unsigned StubSize = 0);
57 SMLoc LastVersionMinDirective;
60 DarwinAsmParser() = default;
62 void Initialize(MCAsmParser &Parser) override {
63 // Call the base implementation.
64 this->MCAsmParserExtension::Initialize(Parser);
66 addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
67 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
70 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
72 ".subsections_via_symbols");
73 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
74 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
75 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
76 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
78 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
80 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
81 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
82 ".secure_log_unique");
83 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
85 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
86 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
88 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
90 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
93 // Special section directives.
94 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
95 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
96 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
98 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
103 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
105 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
111 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
112 ".lazy_symbol_pointer");
113 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
126 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
127 ".non_lazy_symbol_pointer");
129 &DarwinAsmParser::parseSectionDirectiveThreadLocalVariablePointers>(
130 ".thread_local_variable_pointer");
131 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
132 ".objc_cat_cls_meth");
133 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
134 ".objc_cat_inst_meth");
135 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
137 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
139 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
140 ".objc_class_names");
141 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
143 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
145 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
147 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
150 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
151 ".objc_instance_vars");
152 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
153 ".objc_message_refs");
154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
157 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
158 ".objc_meth_var_names");
160 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
161 ".objc_meth_var_types");
162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
163 ".objc_module_info");
164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
167 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
168 ".objc_selector_strs");
170 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
171 ".objc_string_object");
172 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
174 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
176 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
178 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
180 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
182 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
183 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
184 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
185 ".thread_init_func");
186 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
188 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
189 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
190 ".watchos_version_min");
191 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
192 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
193 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
194 ".macosx_version_min");
196 LastVersionMinDirective = SMLoc();
199 bool parseDirectiveAltEntry(StringRef, SMLoc);
200 bool parseDirectiveDesc(StringRef, SMLoc);
201 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
202 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
203 bool parseDirectiveLsym(StringRef, SMLoc);
204 bool parseDirectiveLinkerOption(StringRef, SMLoc);
205 bool parseDirectiveSection(StringRef, SMLoc);
206 bool parseDirectivePushSection(StringRef, SMLoc);
207 bool parseDirectivePopSection(StringRef, SMLoc);
208 bool parseDirectivePrevious(StringRef, SMLoc);
209 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
210 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
211 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
212 bool parseDirectiveTBSS(StringRef, SMLoc);
213 bool parseDirectiveZerofill(StringRef, SMLoc);
214 bool parseDirectiveDataRegion(StringRef, SMLoc);
215 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
217 // Named Section Directive
218 bool parseSectionDirectiveBss(StringRef, SMLoc) {
219 return parseSectionSwitch("__DATA", "__bss");
222 bool parseSectionDirectiveConst(StringRef, SMLoc) {
223 return parseSectionSwitch("__TEXT", "__const");
226 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
227 return parseSectionSwitch("__TEXT", "__static_const");
230 bool parseSectionDirectiveCString(StringRef, SMLoc) {
231 return parseSectionSwitch("__TEXT","__cstring",
232 MachO::S_CSTRING_LITERALS);
235 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
236 return parseSectionSwitch("__TEXT", "__literal4",
237 MachO::S_4BYTE_LITERALS, 4);
240 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
241 return parseSectionSwitch("__TEXT", "__literal8",
242 MachO::S_8BYTE_LITERALS, 8);
245 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
246 return parseSectionSwitch("__TEXT","__literal16",
247 MachO::S_16BYTE_LITERALS, 16);
250 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
251 return parseSectionSwitch("__TEXT","__constructor");
254 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
255 return parseSectionSwitch("__TEXT","__destructor");
258 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
259 return parseSectionSwitch("__TEXT","__fvmlib_init0");
262 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
263 return parseSectionSwitch("__TEXT","__fvmlib_init1");
266 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
267 return parseSectionSwitch("__TEXT","__symbol_stub",
268 MachO::S_SYMBOL_STUBS |
269 MachO::S_ATTR_PURE_INSTRUCTIONS,
270 // FIXME: Different on PPC and ARM.
274 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
275 return parseSectionSwitch("__TEXT","__picsymbol_stub",
276 MachO::S_SYMBOL_STUBS |
277 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
280 bool parseSectionDirectiveData(StringRef, SMLoc) {
281 return parseSectionSwitch("__DATA", "__data");
284 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
285 return parseSectionSwitch("__DATA", "__static_data");
288 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
289 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
290 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
293 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
294 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
295 MachO::S_LAZY_SYMBOL_POINTERS, 4);
298 bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
299 return parseSectionSwitch("__DATA", "__thread_ptr",
300 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
303 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
304 return parseSectionSwitch("__DATA", "__dyld");
307 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
308 return parseSectionSwitch("__DATA", "__mod_init_func",
309 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
312 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
313 return parseSectionSwitch("__DATA", "__mod_term_func",
314 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
317 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
318 return parseSectionSwitch("__DATA", "__const");
321 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
322 return parseSectionSwitch("__OBJC", "__class",
323 MachO::S_ATTR_NO_DEAD_STRIP);
326 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
327 return parseSectionSwitch("__OBJC", "__meta_class",
328 MachO::S_ATTR_NO_DEAD_STRIP);
331 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
332 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
333 MachO::S_ATTR_NO_DEAD_STRIP);
336 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
337 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
338 MachO::S_ATTR_NO_DEAD_STRIP);
341 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
342 return parseSectionSwitch("__OBJC", "__protocol",
343 MachO::S_ATTR_NO_DEAD_STRIP);
346 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
347 return parseSectionSwitch("__OBJC", "__string_object",
348 MachO::S_ATTR_NO_DEAD_STRIP);
351 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
352 return parseSectionSwitch("__OBJC", "__cls_meth",
353 MachO::S_ATTR_NO_DEAD_STRIP);
356 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
357 return parseSectionSwitch("__OBJC", "__inst_meth",
358 MachO::S_ATTR_NO_DEAD_STRIP);
361 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
362 return parseSectionSwitch("__OBJC", "__cls_refs",
363 MachO::S_ATTR_NO_DEAD_STRIP |
364 MachO::S_LITERAL_POINTERS, 4);
367 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
368 return parseSectionSwitch("__OBJC", "__message_refs",
369 MachO::S_ATTR_NO_DEAD_STRIP |
370 MachO::S_LITERAL_POINTERS, 4);
373 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
374 return parseSectionSwitch("__OBJC", "__symbols",
375 MachO::S_ATTR_NO_DEAD_STRIP);
378 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
379 return parseSectionSwitch("__OBJC", "__category",
380 MachO::S_ATTR_NO_DEAD_STRIP);
383 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
384 return parseSectionSwitch("__OBJC", "__class_vars",
385 MachO::S_ATTR_NO_DEAD_STRIP);
388 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
389 return parseSectionSwitch("__OBJC", "__instance_vars",
390 MachO::S_ATTR_NO_DEAD_STRIP);
393 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
394 return parseSectionSwitch("__OBJC", "__module_info",
395 MachO::S_ATTR_NO_DEAD_STRIP);
398 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
399 return parseSectionSwitch("__TEXT", "__cstring",
400 MachO::S_CSTRING_LITERALS);
403 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
404 return parseSectionSwitch("__TEXT", "__cstring",
405 MachO::S_CSTRING_LITERALS);
408 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
409 return parseSectionSwitch("__TEXT", "__cstring",
410 MachO::S_CSTRING_LITERALS);
413 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
414 return parseSectionSwitch("__OBJC", "__selector_strs",
415 MachO::S_CSTRING_LITERALS);
418 bool parseSectionDirectiveTData(StringRef, SMLoc) {
419 return parseSectionSwitch("__DATA", "__thread_data",
420 MachO::S_THREAD_LOCAL_REGULAR);
423 bool parseSectionDirectiveText(StringRef, SMLoc) {
424 return parseSectionSwitch("__TEXT", "__text",
425 MachO::S_ATTR_PURE_INSTRUCTIONS);
428 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
429 return parseSectionSwitch("__DATA", "__thread_vars",
430 MachO::S_THREAD_LOCAL_VARIABLES);
433 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
434 // Darwin silently ignores the .ident directive.
435 getParser().eatToEndOfStatement();
439 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
440 return parseSectionSwitch("__DATA", "__thread_init",
441 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
444 bool parseVersionMin(StringRef, SMLoc);
447 } // end anonymous namespace
449 bool DarwinAsmParser::parseSectionSwitch(StringRef Segment, StringRef Section,
450 unsigned TAA, unsigned Align,
452 if (getLexer().isNot(AsmToken::EndOfStatement))
453 return TokError("unexpected token in section switching directive");
456 // FIXME: Arch specific.
457 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
458 getStreamer().SwitchSection(getContext().getMachOSection(
459 Segment, Section, TAA, StubSize,
460 isText ? SectionKind::getText() : SectionKind::getData()));
462 // Set the implicit alignment, if any.
464 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
465 // alignment on the section (e.g., if one manually inserts bytes into the
466 // section, then just issuing the section switch directive will not realign
467 // the section. However, this is arguably more reasonable behavior, and there
468 // is no good reason for someone to intentionally emit incorrectly sized
469 // values into the implicitly aligned sections.
471 getStreamer().EmitValueToAlignment(Align);
476 /// parseDirectiveAltEntry
477 /// ::= .alt_entry identifier
478 bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
480 if (getParser().parseIdentifier(Name))
481 return TokError("expected identifier in directive");
484 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
486 if (Sym->isDefined())
487 return TokError(".alt_entry must preceed symbol definition");
489 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
490 return TokError("unable to emit symbol attribute");
496 /// parseDirectiveDesc
497 /// ::= .desc identifier , expression
498 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
500 if (getParser().parseIdentifier(Name))
501 return TokError("expected identifier in directive");
503 // Handle the identifier as the key symbol.
504 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
506 if (getLexer().isNot(AsmToken::Comma))
507 return TokError("unexpected token in '.desc' directive");
511 if (getParser().parseAbsoluteExpression(DescValue))
514 if (getLexer().isNot(AsmToken::EndOfStatement))
515 return TokError("unexpected token in '.desc' directive");
519 // Set the n_desc field of this Symbol to this DescValue
520 getStreamer().EmitSymbolDesc(Sym, DescValue);
525 /// parseDirectiveIndirectSymbol
526 /// ::= .indirect_symbol identifier
527 bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
528 const MCSectionMachO *Current = static_cast<const MCSectionMachO *>(
529 getStreamer().getCurrentSectionOnly());
530 MachO::SectionType SectionType = Current->getType();
531 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
532 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
533 SectionType != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
534 SectionType != MachO::S_SYMBOL_STUBS)
535 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
539 if (getParser().parseIdentifier(Name))
540 return TokError("expected identifier in .indirect_symbol directive");
542 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
544 // Assembler local symbols don't make any sense here. Complain loudly.
545 if (Sym->isTemporary())
546 return TokError("non-local symbol required in directive");
548 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
549 return TokError("unable to emit indirect symbol attribute for: " + Name);
551 if (getLexer().isNot(AsmToken::EndOfStatement))
552 return TokError("unexpected token in '.indirect_symbol' directive");
559 /// parseDirectiveDumpOrLoad
560 /// ::= ( .dump | .load ) "filename"
561 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
563 bool IsDump = Directive == ".dump";
564 if (getLexer().isNot(AsmToken::String))
565 return TokError("expected string in '.dump' or '.load' directive");
569 if (getLexer().isNot(AsmToken::EndOfStatement))
570 return TokError("unexpected token in '.dump' or '.load' directive");
574 // FIXME: If/when .dump and .load are implemented they will be done in the
575 // the assembly parser and not have any need for an MCStreamer API.
577 return Warning(IDLoc, "ignoring directive .dump for now");
579 return Warning(IDLoc, "ignoring directive .load for now");
582 /// ParseDirectiveLinkerOption
583 /// ::= .linker_option "string" ( , "string" )*
584 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
585 SmallVector<std::string, 4> Args;
587 if (getLexer().isNot(AsmToken::String))
588 return TokError("expected string in '" + Twine(IDVal) + "' directive");
591 if (getParser().parseEscapedString(Data))
594 Args.push_back(Data);
596 if (getLexer().is(AsmToken::EndOfStatement))
599 if (getLexer().isNot(AsmToken::Comma))
600 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
604 getStreamer().EmitLinkerOptions(Args);
608 /// parseDirectiveLsym
609 /// ::= .lsym identifier , expression
610 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
612 if (getParser().parseIdentifier(Name))
613 return TokError("expected identifier in directive");
615 // Handle the identifier as the key symbol.
616 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
618 if (getLexer().isNot(AsmToken::Comma))
619 return TokError("unexpected token in '.lsym' directive");
623 if (getParser().parseExpression(Value))
626 if (getLexer().isNot(AsmToken::EndOfStatement))
627 return TokError("unexpected token in '.lsym' directive");
631 // We don't currently support this directive.
633 // FIXME: Diagnostic location!
635 return TokError("directive '.lsym' is unsupported");
638 /// parseDirectiveSection:
639 /// ::= .section identifier (',' identifier)*
640 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
641 SMLoc Loc = getLexer().getLoc();
643 StringRef SectionName;
644 if (getParser().parseIdentifier(SectionName))
645 return Error(Loc, "expected identifier after '.section' directive");
647 // Verify there is a following comma.
648 if (!getLexer().is(AsmToken::Comma))
649 return TokError("unexpected token in '.section' directive");
651 std::string SectionSpec = SectionName;
654 // Add all the tokens until the end of the line, ParseSectionSpecifier will
656 StringRef EOL = getLexer().LexUntilEndOfStatement();
657 SectionSpec.append(EOL.begin(), EOL.end());
660 if (getLexer().isNot(AsmToken::EndOfStatement))
661 return TokError("unexpected token in '.section' directive");
664 StringRef Segment, Section;
668 std::string ErrorStr =
669 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
670 TAA, TAAParsed, StubSize);
672 if (!ErrorStr.empty())
673 return Error(Loc, ErrorStr);
675 // Issue a warning if the target is not powerpc and Section is a *coal* section.
676 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
677 Triple::ArchType ArchTy = TT.getArch();
679 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
680 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
681 .Case("__textcoal_nt", "__text")
682 .Case("__const_coal", "__const")
683 .Case("__datacoal_nt", "__data")
686 if (!Section.equals(NonCoalSection)) {
687 StringRef SectionVal(Loc.getPointer());
688 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
689 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
690 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
691 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
692 SMRange(BLoc, ELoc));
693 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
694 "\"", SMRange(BLoc, ELoc));
698 // FIXME: Arch specific.
699 bool isText = Segment == "__TEXT"; // FIXME: Hack.
700 getStreamer().SwitchSection(getContext().getMachOSection(
701 Segment, Section, TAA, StubSize,
702 isText ? SectionKind::getText() : SectionKind::getData()));
706 /// ParseDirectivePushSection:
707 /// ::= .pushsection identifier (',' identifier)*
708 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
709 getStreamer().PushSection();
711 if (parseDirectiveSection(S, Loc)) {
712 getStreamer().PopSection();
719 /// ParseDirectivePopSection:
721 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
722 if (!getStreamer().PopSection())
723 return TokError(".popsection without corresponding .pushsection");
727 /// ParseDirectivePrevious:
729 bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
730 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
731 if (!PreviousSection.first)
732 return TokError(".previous without corresponding .section");
733 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
737 /// ParseDirectiveSecureLogUnique
738 /// ::= .secure_log_unique ... message ...
739 bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
740 StringRef LogMessage = getParser().parseStringToEndOfStatement();
741 if (getLexer().isNot(AsmToken::EndOfStatement))
742 return TokError("unexpected token in '.secure_log_unique' directive");
744 if (getContext().getSecureLogUsed())
745 return Error(IDLoc, ".secure_log_unique specified multiple times");
747 // Get the secure log path.
748 const char *SecureLogFile = getContext().getSecureLogFile();
750 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
751 "environment variable unset.");
753 // Open the secure log file if we haven't already.
754 raw_fd_ostream *OS = getContext().getSecureLog();
757 auto NewOS = llvm::make_unique<raw_fd_ostream>(
758 StringRef(SecureLogFile), EC, sys::fs::F_Append | sys::fs::F_Text);
760 return Error(IDLoc, Twine("can't open secure log file: ") +
761 SecureLogFile + " (" + EC.message() + ")");
763 getContext().setSecureLog(std::move(NewOS));
766 // Write the message.
767 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
768 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
769 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
770 << LogMessage + "\n";
772 getContext().setSecureLogUsed(true);
777 /// ParseDirectiveSecureLogReset
778 /// ::= .secure_log_reset
779 bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
780 if (getLexer().isNot(AsmToken::EndOfStatement))
781 return TokError("unexpected token in '.secure_log_reset' directive");
785 getContext().setSecureLogUsed(false);
790 /// parseDirectiveSubsectionsViaSymbols
791 /// ::= .subsections_via_symbols
792 bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
793 if (getLexer().isNot(AsmToken::EndOfStatement))
794 return TokError("unexpected token in '.subsections_via_symbols' directive");
798 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
803 /// ParseDirectiveTBSS
804 /// ::= .tbss identifier, size, align
805 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
806 SMLoc IDLoc = getLexer().getLoc();
808 if (getParser().parseIdentifier(Name))
809 return TokError("expected identifier in directive");
811 // Handle the identifier as the key symbol.
812 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
814 if (getLexer().isNot(AsmToken::Comma))
815 return TokError("unexpected token in directive");
819 SMLoc SizeLoc = getLexer().getLoc();
820 if (getParser().parseAbsoluteExpression(Size))
823 int64_t Pow2Alignment = 0;
824 SMLoc Pow2AlignmentLoc;
825 if (getLexer().is(AsmToken::Comma)) {
827 Pow2AlignmentLoc = getLexer().getLoc();
828 if (getParser().parseAbsoluteExpression(Pow2Alignment))
832 if (getLexer().isNot(AsmToken::EndOfStatement))
833 return TokError("unexpected token in '.tbss' directive");
838 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
841 // FIXME: Diagnose overflow.
842 if (Pow2Alignment < 0)
843 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
846 if (!Sym->isUndefined())
847 return Error(IDLoc, "invalid symbol redefinition");
849 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
850 "__DATA", "__thread_bss",
851 MachO::S_THREAD_LOCAL_ZEROFILL,
852 0, SectionKind::getThreadBSS()),
853 Sym, Size, 1 << Pow2Alignment);
858 /// ParseDirectiveZerofill
859 /// ::= .zerofill segname , sectname [, identifier , size_expression [
860 /// , align_expression ]]
861 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
863 if (getParser().parseIdentifier(Segment))
864 return TokError("expected segment name after '.zerofill' directive");
866 if (getLexer().isNot(AsmToken::Comma))
867 return TokError("unexpected token in directive");
871 if (getParser().parseIdentifier(Section))
872 return TokError("expected section name after comma in '.zerofill' "
875 // If this is the end of the line all that was wanted was to create the
876 // the section but with no symbol.
877 if (getLexer().is(AsmToken::EndOfStatement)) {
878 // Create the zerofill section but no symbol
879 getStreamer().EmitZerofill(getContext().getMachOSection(
880 Segment, Section, MachO::S_ZEROFILL,
881 0, SectionKind::getBSS()));
885 if (getLexer().isNot(AsmToken::Comma))
886 return TokError("unexpected token in directive");
889 SMLoc IDLoc = getLexer().getLoc();
891 if (getParser().parseIdentifier(IDStr))
892 return TokError("expected identifier in directive");
894 // handle the identifier as the key symbol.
895 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
897 if (getLexer().isNot(AsmToken::Comma))
898 return TokError("unexpected token in directive");
902 SMLoc SizeLoc = getLexer().getLoc();
903 if (getParser().parseAbsoluteExpression(Size))
906 int64_t Pow2Alignment = 0;
907 SMLoc Pow2AlignmentLoc;
908 if (getLexer().is(AsmToken::Comma)) {
910 Pow2AlignmentLoc = getLexer().getLoc();
911 if (getParser().parseAbsoluteExpression(Pow2Alignment))
915 if (getLexer().isNot(AsmToken::EndOfStatement))
916 return TokError("unexpected token in '.zerofill' directive");
921 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
924 // NOTE: The alignment in the directive is a power of 2 value, the assembler
925 // may internally end up wanting an alignment in bytes.
926 // FIXME: Diagnose overflow.
927 if (Pow2Alignment < 0)
928 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
929 "can't be less than zero");
931 if (!Sym->isUndefined())
932 return Error(IDLoc, "invalid symbol redefinition");
934 // Create the zerofill Symbol with Size and Pow2Alignment
936 // FIXME: Arch specific.
937 getStreamer().EmitZerofill(getContext().getMachOSection(
938 Segment, Section, MachO::S_ZEROFILL,
939 0, SectionKind::getBSS()),
940 Sym, Size, 1 << Pow2Alignment);
945 /// ParseDirectiveDataRegion
946 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
947 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
948 if (getLexer().is(AsmToken::EndOfStatement)) {
950 getStreamer().EmitDataRegion(MCDR_DataRegion);
953 StringRef RegionType;
954 SMLoc Loc = getParser().getTok().getLoc();
955 if (getParser().parseIdentifier(RegionType))
956 return TokError("expected region type after '.data_region' directive");
957 int Kind = StringSwitch<int>(RegionType)
958 .Case("jt8", MCDR_DataRegionJT8)
959 .Case("jt16", MCDR_DataRegionJT16)
960 .Case("jt32", MCDR_DataRegionJT32)
963 return Error(Loc, "unknown region type in '.data_region' directive");
966 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
970 /// ParseDirectiveDataRegionEnd
971 /// ::= .end_data_region
972 bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
973 if (getLexer().isNot(AsmToken::EndOfStatement))
974 return TokError("unexpected token in '.end_data_region' directive");
977 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
982 /// ::= .ios_version_min major,minor[,update]
983 /// ::= .macosx_version_min major,minor[,update]
984 bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
985 int64_t Major = 0, Minor = 0, Update = 0;
986 int Kind = StringSwitch<int>(Directive)
987 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
988 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
989 .Case(".ios_version_min", MCVM_IOSVersionMin)
990 .Case(".macosx_version_min", MCVM_OSXVersionMin);
991 // Get the major version number.
992 if (getLexer().isNot(AsmToken::Integer))
993 return TokError("invalid OS major version number");
994 Major = getLexer().getTok().getIntVal();
995 if (Major > 65535 || Major <= 0)
996 return TokError("invalid OS major version number");
998 if (getLexer().isNot(AsmToken::Comma))
999 return TokError("minor OS version number required, comma expected");
1001 // Get the minor version number.
1002 if (getLexer().isNot(AsmToken::Integer))
1003 return TokError("invalid OS minor version number");
1004 Minor = getLexer().getTok().getIntVal();
1005 if (Minor > 255 || Minor < 0)
1006 return TokError("invalid OS minor version number");
1008 // Get the update level, if specified
1009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1010 if (getLexer().isNot(AsmToken::Comma))
1011 return TokError("invalid update specifier, comma expected");
1013 if (getLexer().isNot(AsmToken::Integer))
1014 return TokError("invalid OS update number");
1015 Update = getLexer().getTok().getIntVal();
1016 if (Update > 255 || Update < 0)
1017 return TokError("invalid OS update number");
1021 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
1022 Triple::OSType ExpectedOS = Triple::UnknownOS;
1023 switch ((MCVersionMinType)Kind) {
1024 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
1025 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
1026 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
1027 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
1029 if (T.getOS() != ExpectedOS)
1030 Warning(Loc, Directive + " should only be used for " +
1031 Triple::getOSTypeName(ExpectedOS) + " targets");
1033 if (LastVersionMinDirective.isValid()) {
1034 Warning(Loc, "overriding previous version_min directive");
1035 Note(LastVersionMinDirective, "previous definition is here");
1037 LastVersionMinDirective = Loc;
1039 // We've parsed a correct version specifier, so send it to the streamer.
1040 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
1047 MCAsmParserExtension *createDarwinAsmParser() {
1048 return new DarwinAsmParser;
1051 } // end llvm namespace