OSDN Git Service

73a7ad0500c372a4fc8a658e22bb4f80ed29a63a
[android-x86/external-llvm.git] / lib / MC / MCParser / DarwinAsmParser.cpp
1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
32 #include <algorithm>
33 #include <cstddef>
34 #include <cstdint>
35 #include <string>
36 #include <system_error>
37 #include <utility>
38
39 using namespace llvm;
40
41 namespace {
42
43 /// \brief Implementation of directive handling which is shared across all
44 /// Darwin targets.
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);
51   }
52
53   bool parseSectionSwitch(StringRef Segment, StringRef Section,
54                           unsigned TAA = 0, unsigned ImplicitAlign = 0,
55                           unsigned StubSize = 0);
56
57   SMLoc LastVersionMinDirective;
58
59 public:
60   DarwinAsmParser() = default;
61
62   void Initialize(MCAsmParser &Parser) override {
63     // Call the base implementation.
64     this->MCAsmParserExtension::Initialize(Parser);
65
66     addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
67     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
68     addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
69       ".indirect_symbol");
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>(
77       ".pushsection");
78     addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
79       ".popsection");
80     addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
81     addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
82       ".secure_log_unique");
83     addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
84       ".secure_log_reset");
85     addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
86     addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
87
88     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
89       ".data_region");
90     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
91       ".end_data_region");
92
93     // Special section directives.
94     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
95     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
96     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
97       ".const_data");
98     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
99       ".constructor");
100     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
101       ".cstring");
102     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
103     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
104       ".destructor");
105     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
106     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
107       ".fvmlib_init0");
108     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
109       ".fvmlib_init1");
110     addDirectiveHandler<
111       &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
112         ".lazy_symbol_pointer");
113     addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
114       ".linker_option");
115     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
116       ".literal16");
117     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
118       ".literal4");
119     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
120       ".literal8");
121     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
122       ".mod_init_func");
123     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
124       ".mod_term_func");
125     addDirectiveHandler<
126       &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
127         ".non_lazy_symbol_pointer");
128     addDirectiveHandler<
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>(
136       ".objc_category");
137     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
138       ".objc_class");
139     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
140       ".objc_class_names");
141     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
142       ".objc_class_vars");
143     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
144       ".objc_cls_meth");
145     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
146       ".objc_cls_refs");
147     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
148       ".objc_inst_meth");
149     addDirectiveHandler<
150       &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
151         ".objc_instance_vars");
152     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
153       ".objc_message_refs");
154     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
155       ".objc_meta_class");
156     addDirectiveHandler<
157       &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
158         ".objc_meth_var_names");
159     addDirectiveHandler<
160       &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
161         ".objc_meth_var_types");
162     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
163       ".objc_module_info");
164     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
165       ".objc_protocol");
166     addDirectiveHandler<
167       &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
168         ".objc_selector_strs");
169     addDirectiveHandler<
170       &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
171         ".objc_string_object");
172     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
173       ".objc_symbols");
174     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
175       ".picsymbol_stub");
176     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
177       ".static_const");
178     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
179       ".static_data");
180     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
181       ".symbol_stub");
182     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
183     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
184     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
185       ".thread_init_func");
186     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
187
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");
195
196     LastVersionMinDirective = SMLoc();
197   }
198
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);
216
217   // Named Section Directive
218   bool parseSectionDirectiveBss(StringRef, SMLoc) {
219     return parseSectionSwitch("__DATA", "__bss");
220   }
221
222   bool parseSectionDirectiveConst(StringRef, SMLoc) {
223     return parseSectionSwitch("__TEXT", "__const");
224   }
225
226   bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
227     return parseSectionSwitch("__TEXT", "__static_const");
228   }
229
230   bool parseSectionDirectiveCString(StringRef, SMLoc) {
231     return parseSectionSwitch("__TEXT","__cstring",
232                               MachO::S_CSTRING_LITERALS);
233   }
234
235   bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
236     return parseSectionSwitch("__TEXT", "__literal4",
237                               MachO::S_4BYTE_LITERALS, 4);
238   }
239
240   bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
241     return parseSectionSwitch("__TEXT", "__literal8",
242                               MachO::S_8BYTE_LITERALS, 8);
243   }
244
245   bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
246     return parseSectionSwitch("__TEXT","__literal16",
247                               MachO::S_16BYTE_LITERALS, 16);
248   }
249
250   bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
251     return parseSectionSwitch("__TEXT","__constructor");
252   }
253
254   bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
255     return parseSectionSwitch("__TEXT","__destructor");
256   }
257
258   bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
259     return parseSectionSwitch("__TEXT","__fvmlib_init0");
260   }
261
262   bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
263     return parseSectionSwitch("__TEXT","__fvmlib_init1");
264   }
265
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.
271                               0, 16);
272   }
273
274   bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
275     return parseSectionSwitch("__TEXT","__picsymbol_stub",
276                               MachO::S_SYMBOL_STUBS |
277                               MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
278   }
279
280   bool parseSectionDirectiveData(StringRef, SMLoc) {
281     return parseSectionSwitch("__DATA", "__data");
282   }
283
284   bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
285     return parseSectionSwitch("__DATA", "__static_data");
286   }
287
288   bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
289     return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
290                               MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
291   }
292
293   bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
294     return parseSectionSwitch("__DATA", "__la_symbol_ptr",
295                               MachO::S_LAZY_SYMBOL_POINTERS, 4);
296   }
297
298   bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
299     return parseSectionSwitch("__DATA", "__thread_ptr",
300                               MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
301   }
302
303   bool parseSectionDirectiveDyld(StringRef, SMLoc) {
304     return parseSectionSwitch("__DATA", "__dyld");
305   }
306
307   bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
308     return parseSectionSwitch("__DATA", "__mod_init_func",
309                               MachO::S_MOD_INIT_FUNC_POINTERS, 4);
310   }
311
312   bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
313     return parseSectionSwitch("__DATA", "__mod_term_func",
314                               MachO::S_MOD_TERM_FUNC_POINTERS, 4);
315   }
316
317   bool parseSectionDirectiveConstData(StringRef, SMLoc) {
318     return parseSectionSwitch("__DATA", "__const");
319   }
320
321   bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
322     return parseSectionSwitch("__OBJC", "__class",
323                               MachO::S_ATTR_NO_DEAD_STRIP);
324   }
325
326   bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
327     return parseSectionSwitch("__OBJC", "__meta_class",
328                               MachO::S_ATTR_NO_DEAD_STRIP);
329   }
330
331   bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
332     return parseSectionSwitch("__OBJC", "__cat_cls_meth",
333                               MachO::S_ATTR_NO_DEAD_STRIP);
334   }
335
336   bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
337     return parseSectionSwitch("__OBJC", "__cat_inst_meth",
338                               MachO::S_ATTR_NO_DEAD_STRIP);
339   }
340
341   bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
342     return parseSectionSwitch("__OBJC", "__protocol",
343                               MachO::S_ATTR_NO_DEAD_STRIP);
344   }
345
346   bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
347     return parseSectionSwitch("__OBJC", "__string_object",
348                               MachO::S_ATTR_NO_DEAD_STRIP);
349   }
350
351   bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
352     return parseSectionSwitch("__OBJC", "__cls_meth",
353                               MachO::S_ATTR_NO_DEAD_STRIP);
354   }
355
356   bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
357     return parseSectionSwitch("__OBJC", "__inst_meth",
358                               MachO::S_ATTR_NO_DEAD_STRIP);
359   }
360
361   bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
362     return parseSectionSwitch("__OBJC", "__cls_refs",
363                               MachO::S_ATTR_NO_DEAD_STRIP |
364                               MachO::S_LITERAL_POINTERS, 4);
365   }
366
367   bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
368     return parseSectionSwitch("__OBJC", "__message_refs",
369                               MachO::S_ATTR_NO_DEAD_STRIP |
370                               MachO::S_LITERAL_POINTERS, 4);
371   }
372
373   bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
374     return parseSectionSwitch("__OBJC", "__symbols",
375                               MachO::S_ATTR_NO_DEAD_STRIP);
376   }
377
378   bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
379     return parseSectionSwitch("__OBJC", "__category",
380                               MachO::S_ATTR_NO_DEAD_STRIP);
381   }
382
383   bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
384     return parseSectionSwitch("__OBJC", "__class_vars",
385                               MachO::S_ATTR_NO_DEAD_STRIP);
386   }
387
388   bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
389     return parseSectionSwitch("__OBJC", "__instance_vars",
390                               MachO::S_ATTR_NO_DEAD_STRIP);
391   }
392
393   bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
394     return parseSectionSwitch("__OBJC", "__module_info",
395                               MachO::S_ATTR_NO_DEAD_STRIP);
396   }
397
398   bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
399     return parseSectionSwitch("__TEXT", "__cstring",
400                               MachO::S_CSTRING_LITERALS);
401   }
402
403   bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
404     return parseSectionSwitch("__TEXT", "__cstring",
405                               MachO::S_CSTRING_LITERALS);
406   }
407
408   bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
409     return parseSectionSwitch("__TEXT", "__cstring",
410                               MachO::S_CSTRING_LITERALS);
411   }
412
413   bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
414     return parseSectionSwitch("__OBJC", "__selector_strs",
415                               MachO::S_CSTRING_LITERALS);
416   }
417
418   bool parseSectionDirectiveTData(StringRef, SMLoc) {
419     return parseSectionSwitch("__DATA", "__thread_data",
420                               MachO::S_THREAD_LOCAL_REGULAR);
421   }
422
423   bool parseSectionDirectiveText(StringRef, SMLoc) {
424     return parseSectionSwitch("__TEXT", "__text",
425                               MachO::S_ATTR_PURE_INSTRUCTIONS);
426   }
427
428   bool parseSectionDirectiveTLV(StringRef, SMLoc) {
429     return parseSectionSwitch("__DATA", "__thread_vars",
430                               MachO::S_THREAD_LOCAL_VARIABLES);
431   }
432
433   bool parseSectionDirectiveIdent(StringRef, SMLoc) {
434     // Darwin silently ignores the .ident directive.
435     getParser().eatToEndOfStatement();
436     return false;
437   }
438
439   bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
440     return parseSectionSwitch("__DATA", "__thread_init",
441                          MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
442   }
443
444   bool parseVersionMin(StringRef, SMLoc);
445 };
446
447 } // end anonymous namespace
448
449 bool DarwinAsmParser::parseSectionSwitch(StringRef Segment, StringRef Section,
450                                          unsigned TAA, unsigned Align,
451                                          unsigned StubSize) {
452   if (getLexer().isNot(AsmToken::EndOfStatement))
453     return TokError("unexpected token in section switching directive");
454   Lex();
455
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()));
461
462   // Set the implicit alignment, if any.
463   //
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.
470   if (Align)
471     getStreamer().EmitValueToAlignment(Align);
472
473   return false;
474 }
475
476 /// parseDirectiveAltEntry
477 ///  ::= .alt_entry identifier
478 bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
479   StringRef Name;
480   if (getParser().parseIdentifier(Name))
481     return TokError("expected identifier in directive");
482
483   // Look up symbol.
484   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
485
486   if (Sym->isDefined())
487     return TokError(".alt_entry must preceed symbol definition");
488
489   if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
490     return TokError("unable to emit symbol attribute");
491
492   Lex();
493   return false;
494 }
495
496 /// parseDirectiveDesc
497 ///  ::= .desc identifier , expression
498 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
499   StringRef Name;
500   if (getParser().parseIdentifier(Name))
501     return TokError("expected identifier in directive");
502
503   // Handle the identifier as the key symbol.
504   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
505
506   if (getLexer().isNot(AsmToken::Comma))
507     return TokError("unexpected token in '.desc' directive");
508   Lex();
509
510   int64_t DescValue;
511   if (getParser().parseAbsoluteExpression(DescValue))
512     return true;
513
514   if (getLexer().isNot(AsmToken::EndOfStatement))
515     return TokError("unexpected token in '.desc' directive");
516
517   Lex();
518
519   // Set the n_desc field of this Symbol to this DescValue
520   getStreamer().EmitSymbolDesc(Sym, DescValue);
521
522   return false;
523 }
524
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 "
536                       "section");
537
538   StringRef Name;
539   if (getParser().parseIdentifier(Name))
540     return TokError("expected identifier in .indirect_symbol directive");
541
542   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
543
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");
547
548   if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
549     return TokError("unable to emit indirect symbol attribute for: " + Name);
550
551   if (getLexer().isNot(AsmToken::EndOfStatement))
552     return TokError("unexpected token in '.indirect_symbol' directive");
553
554   Lex();
555
556   return false;
557 }
558
559 /// parseDirectiveDumpOrLoad
560 ///  ::= ( .dump | .load ) "filename"
561 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
562                                                SMLoc IDLoc) {
563   bool IsDump = Directive == ".dump";
564   if (getLexer().isNot(AsmToken::String))
565     return TokError("expected string in '.dump' or '.load' directive");
566
567   Lex();
568
569   if (getLexer().isNot(AsmToken::EndOfStatement))
570     return TokError("unexpected token in '.dump' or '.load' directive");
571
572   Lex();
573
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.
576   if (IsDump)
577     return Warning(IDLoc, "ignoring directive .dump for now");
578   else
579     return Warning(IDLoc, "ignoring directive .load for now");
580 }
581
582 /// ParseDirectiveLinkerOption
583 ///  ::= .linker_option "string" ( , "string" )*
584 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
585   SmallVector<std::string, 4> Args;
586   while (true) {
587     if (getLexer().isNot(AsmToken::String))
588       return TokError("expected string in '" + Twine(IDVal) + "' directive");
589
590     std::string Data;
591     if (getParser().parseEscapedString(Data))
592       return true;
593
594     Args.push_back(Data);
595
596     if (getLexer().is(AsmToken::EndOfStatement))
597       break;
598
599     if (getLexer().isNot(AsmToken::Comma))
600       return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
601     Lex();
602   }
603
604   getStreamer().EmitLinkerOptions(Args);
605   return false;
606 }
607
608 /// parseDirectiveLsym
609 ///  ::= .lsym identifier , expression
610 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
611   StringRef Name;
612   if (getParser().parseIdentifier(Name))
613     return TokError("expected identifier in directive");
614
615   // Handle the identifier as the key symbol.
616   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
617
618   if (getLexer().isNot(AsmToken::Comma))
619     return TokError("unexpected token in '.lsym' directive");
620   Lex();
621
622   const MCExpr *Value;
623   if (getParser().parseExpression(Value))
624     return true;
625
626   if (getLexer().isNot(AsmToken::EndOfStatement))
627     return TokError("unexpected token in '.lsym' directive");
628
629   Lex();
630
631   // We don't currently support this directive.
632   //
633   // FIXME: Diagnostic location!
634   (void) Sym;
635   return TokError("directive '.lsym' is unsupported");
636 }
637
638 /// parseDirectiveSection:
639 ///   ::= .section identifier (',' identifier)*
640 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
641   SMLoc Loc = getLexer().getLoc();
642
643   StringRef SectionName;
644   if (getParser().parseIdentifier(SectionName))
645     return Error(Loc, "expected identifier after '.section' directive");
646
647   // Verify there is a following comma.
648   if (!getLexer().is(AsmToken::Comma))
649     return TokError("unexpected token in '.section' directive");
650
651   std::string SectionSpec = SectionName;
652   SectionSpec += ",";
653
654   // Add all the tokens until the end of the line, ParseSectionSpecifier will
655   // handle this.
656   StringRef EOL = getLexer().LexUntilEndOfStatement();
657   SectionSpec.append(EOL.begin(), EOL.end());
658
659   Lex();
660   if (getLexer().isNot(AsmToken::EndOfStatement))
661     return TokError("unexpected token in '.section' directive");
662   Lex();
663
664   StringRef Segment, Section;
665   unsigned StubSize;
666   unsigned TAA;
667   bool TAAParsed;
668   std::string ErrorStr =
669     MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
670                                           TAA, TAAParsed, StubSize);
671
672   if (!ErrorStr.empty())
673     return Error(Loc, ErrorStr);
674
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();
678
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")
684                                    .Default(Section);
685
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));
695     }
696   }
697
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()));
703   return false;
704 }
705
706 /// ParseDirectivePushSection:
707 ///   ::= .pushsection identifier (',' identifier)*
708 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
709   getStreamer().PushSection();
710
711   if (parseDirectiveSection(S, Loc)) {
712     getStreamer().PopSection();
713     return true;
714   }
715
716   return false;
717 }
718
719 /// ParseDirectivePopSection:
720 ///   ::= .popsection
721 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
722   if (!getStreamer().PopSection())
723     return TokError(".popsection without corresponding .pushsection");
724   return false;
725 }
726
727 /// ParseDirectivePrevious:
728 ///   ::= .previous
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);
734   return false;
735 }
736
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");
743
744   if (getContext().getSecureLogUsed())
745     return Error(IDLoc, ".secure_log_unique specified multiple times");
746
747   // Get the secure log path.
748   const char *SecureLogFile = getContext().getSecureLogFile();
749   if (!SecureLogFile)
750     return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
751                  "environment variable unset.");
752
753   // Open the secure log file if we haven't already.
754   raw_fd_ostream *OS = getContext().getSecureLog();
755   if (!OS) {
756     std::error_code EC;
757     auto NewOS = llvm::make_unique<raw_fd_ostream>(
758         StringRef(SecureLogFile), EC, sys::fs::F_Append | sys::fs::F_Text);
759     if (EC)
760        return Error(IDLoc, Twine("can't open secure log file: ") +
761                                SecureLogFile + " (" + EC.message() + ")");
762     OS = NewOS.get();
763     getContext().setSecureLog(std::move(NewOS));
764   }
765
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";
771
772   getContext().setSecureLogUsed(true);
773
774   return false;
775 }
776
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");
782
783   Lex();
784
785   getContext().setSecureLogUsed(false);
786
787   return false;
788 }
789
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");
795
796   Lex();
797
798   getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
799
800   return false;
801 }
802
803 /// ParseDirectiveTBSS
804 ///  ::= .tbss identifier, size, align
805 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
806   SMLoc IDLoc = getLexer().getLoc();
807   StringRef Name;
808   if (getParser().parseIdentifier(Name))
809     return TokError("expected identifier in directive");
810
811   // Handle the identifier as the key symbol.
812   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
813
814   if (getLexer().isNot(AsmToken::Comma))
815     return TokError("unexpected token in directive");
816   Lex();
817
818   int64_t Size;
819   SMLoc SizeLoc = getLexer().getLoc();
820   if (getParser().parseAbsoluteExpression(Size))
821     return true;
822
823   int64_t Pow2Alignment = 0;
824   SMLoc Pow2AlignmentLoc;
825   if (getLexer().is(AsmToken::Comma)) {
826     Lex();
827     Pow2AlignmentLoc = getLexer().getLoc();
828     if (getParser().parseAbsoluteExpression(Pow2Alignment))
829       return true;
830   }
831
832   if (getLexer().isNot(AsmToken::EndOfStatement))
833     return TokError("unexpected token in '.tbss' directive");
834
835   Lex();
836
837   if (Size < 0)
838     return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
839                  "zero");
840
841   // FIXME: Diagnose overflow.
842   if (Pow2Alignment < 0)
843     return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
844                  "than zero");
845
846   if (!Sym->isUndefined())
847     return Error(IDLoc, "invalid symbol redefinition");
848
849   getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
850                                  "__DATA", "__thread_bss",
851                                  MachO::S_THREAD_LOCAL_ZEROFILL,
852                                  0, SectionKind::getThreadBSS()),
853                                Sym, Size, 1 << Pow2Alignment);
854
855   return false;
856 }
857
858 /// ParseDirectiveZerofill
859 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
860 ///      , align_expression ]]
861 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
862   StringRef Segment;
863   if (getParser().parseIdentifier(Segment))
864     return TokError("expected segment name after '.zerofill' directive");
865
866   if (getLexer().isNot(AsmToken::Comma))
867     return TokError("unexpected token in directive");
868   Lex();
869
870   StringRef Section;
871   if (getParser().parseIdentifier(Section))
872     return TokError("expected section name after comma in '.zerofill' "
873                     "directive");
874
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()));
882     return false;
883   }
884
885   if (getLexer().isNot(AsmToken::Comma))
886     return TokError("unexpected token in directive");
887   Lex();
888
889   SMLoc IDLoc = getLexer().getLoc();
890   StringRef IDStr;
891   if (getParser().parseIdentifier(IDStr))
892     return TokError("expected identifier in directive");
893
894   // handle the identifier as the key symbol.
895   MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
896
897   if (getLexer().isNot(AsmToken::Comma))
898     return TokError("unexpected token in directive");
899   Lex();
900
901   int64_t Size;
902   SMLoc SizeLoc = getLexer().getLoc();
903   if (getParser().parseAbsoluteExpression(Size))
904     return true;
905
906   int64_t Pow2Alignment = 0;
907   SMLoc Pow2AlignmentLoc;
908   if (getLexer().is(AsmToken::Comma)) {
909     Lex();
910     Pow2AlignmentLoc = getLexer().getLoc();
911     if (getParser().parseAbsoluteExpression(Pow2Alignment))
912       return true;
913   }
914
915   if (getLexer().isNot(AsmToken::EndOfStatement))
916     return TokError("unexpected token in '.zerofill' directive");
917
918   Lex();
919
920   if (Size < 0)
921     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
922                  "than zero");
923
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");
930
931   if (!Sym->isUndefined())
932     return Error(IDLoc, "invalid symbol redefinition");
933
934   // Create the zerofill Symbol with Size and Pow2Alignment
935   //
936   // FIXME: Arch specific.
937   getStreamer().EmitZerofill(getContext().getMachOSection(
938                                Segment, Section, MachO::S_ZEROFILL,
939                                0, SectionKind::getBSS()),
940                              Sym, Size, 1 << Pow2Alignment);
941
942   return false;
943 }
944
945 /// ParseDirectiveDataRegion
946 ///  ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
947 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
948   if (getLexer().is(AsmToken::EndOfStatement)) {
949     Lex();
950     getStreamer().EmitDataRegion(MCDR_DataRegion);
951     return false;
952   }
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)
961     .Default(-1);
962   if (Kind == -1)
963     return Error(Loc, "unknown region type in '.data_region' directive");
964   Lex();
965
966   getStreamer().EmitDataRegion((MCDataRegionType)Kind);
967   return false;
968 }
969
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");
975
976   Lex();
977   getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
978   return false;
979 }
980
981 /// parseVersionMin
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");
997   Lex();
998   if (getLexer().isNot(AsmToken::Comma))
999     return TokError("minor OS version number required, comma expected");
1000   Lex();
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");
1007   Lex();
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");
1012     Lex();
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");
1018     Lex();
1019   }
1020
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;
1028   }
1029   if (T.getOS() != ExpectedOS)
1030     Warning(Loc, Directive + " should only be used for " +
1031             Triple::getOSTypeName(ExpectedOS) + " targets");
1032
1033   if (LastVersionMinDirective.isValid()) {
1034     Warning(Loc, "overriding previous version_min directive");
1035     Note(LastVersionMinDirective, "previous definition is here");
1036   }
1037   LastVersionMinDirective = Loc;
1038
1039   // We've parsed a correct version specifier, so send it to the streamer.
1040   getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
1041
1042   return false;
1043 }
1044
1045 namespace llvm {
1046
1047 MCAsmParserExtension *createDarwinAsmParser() {
1048   return new DarwinAsmParser;
1049 }
1050
1051 } // end llvm namespace