LLVM  3.7.0
DarwinAsmParser.cpp
Go to the documentation of this file.
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 
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/SourceMgr.h"
23 using namespace llvm;
24 
25 namespace {
26 
27 /// \brief Implementation of directive handling which is shared across all
28 /// Darwin targets.
29 class DarwinAsmParser : public MCAsmParserExtension {
30  template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
31  void addDirectiveHandler(StringRef Directive) {
32  MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
33  this, HandleDirective<DarwinAsmParser, HandlerMethod>);
34  getParser().addDirectiveHandler(Directive, Handler);
35  }
36 
37  bool parseSectionSwitch(const char *Segment, const char *Section,
38  unsigned TAA = 0, unsigned ImplicitAlign = 0,
39  unsigned StubSize = 0);
40 
41 public:
42  DarwinAsmParser() {}
43 
44  void Initialize(MCAsmParser &Parser) override {
45  // Call the base implementation.
47 
48  addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
49  addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
50  ".indirect_symbol");
51  addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
52  addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
53  ".subsections_via_symbols");
54  addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
55  addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
56  addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
57  addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
58  ".pushsection");
59  addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
60  ".popsection");
61  addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
62  addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
63  ".secure_log_unique");
64  addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
65  ".secure_log_reset");
66  addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
67  addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
68 
69  addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
70  ".data_region");
71  addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
72  ".end_data_region");
73 
74  // Special section directives.
75  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
76  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
77  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
78  ".const_data");
79  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
80  ".constructor");
81  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
82  ".cstring");
83  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
84  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
85  ".destructor");
86  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
87  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
88  ".fvmlib_init0");
89  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
90  ".fvmlib_init1");
91  addDirectiveHandler<
92  &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
93  ".lazy_symbol_pointer");
94  addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
95  ".linker_option");
96  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
97  ".literal16");
98  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
99  ".literal4");
100  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
101  ".literal8");
102  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
103  ".mod_init_func");
104  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
105  ".mod_term_func");
106  addDirectiveHandler<
107  &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
108  ".non_lazy_symbol_pointer");
109  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
110  ".objc_cat_cls_meth");
111  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
112  ".objc_cat_inst_meth");
113  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
114  ".objc_category");
115  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
116  ".objc_class");
117  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
118  ".objc_class_names");
119  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
120  ".objc_class_vars");
121  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
122  ".objc_cls_meth");
123  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
124  ".objc_cls_refs");
125  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
126  ".objc_inst_meth");
127  addDirectiveHandler<
128  &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
129  ".objc_instance_vars");
130  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
131  ".objc_message_refs");
132  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
133  ".objc_meta_class");
134  addDirectiveHandler<
135  &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
136  ".objc_meth_var_names");
137  addDirectiveHandler<
138  &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
139  ".objc_meth_var_types");
140  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
141  ".objc_module_info");
142  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
143  ".objc_protocol");
144  addDirectiveHandler<
145  &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
146  ".objc_selector_strs");
147  addDirectiveHandler<
148  &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
149  ".objc_string_object");
150  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
151  ".objc_symbols");
152  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
153  ".picsymbol_stub");
154  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
155  ".static_const");
156  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
157  ".static_data");
158  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
159  ".symbol_stub");
160  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
161  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
162  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
163  ".thread_init_func");
164  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
165 
166  addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
167  addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
168  addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
169  ".macosx_version_min");
170  }
171 
172  bool parseDirectiveDesc(StringRef, SMLoc);
173  bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
174  bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
175  bool parseDirectiveLsym(StringRef, SMLoc);
176  bool parseDirectiveLinkerOption(StringRef, SMLoc);
177  bool parseDirectiveSection(StringRef, SMLoc);
178  bool parseDirectivePushSection(StringRef, SMLoc);
179  bool parseDirectivePopSection(StringRef, SMLoc);
180  bool parseDirectivePrevious(StringRef, SMLoc);
181  bool parseDirectiveSecureLogReset(StringRef, SMLoc);
182  bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
183  bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
184  bool parseDirectiveTBSS(StringRef, SMLoc);
185  bool parseDirectiveZerofill(StringRef, SMLoc);
186  bool parseDirectiveDataRegion(StringRef, SMLoc);
187  bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
188 
189  // Named Section Directive
190  bool parseSectionDirectiveBss(StringRef, SMLoc) {
191  return parseSectionSwitch("__DATA", "__bss");
192  }
193 
194  bool parseSectionDirectiveConst(StringRef, SMLoc) {
195  return parseSectionSwitch("__TEXT", "__const");
196  }
197  bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
198  return parseSectionSwitch("__TEXT", "__static_const");
199  }
200  bool parseSectionDirectiveCString(StringRef, SMLoc) {
201  return parseSectionSwitch("__TEXT","__cstring",
203  }
204  bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
205  return parseSectionSwitch("__TEXT", "__literal4",
207  }
208  bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
209  return parseSectionSwitch("__TEXT", "__literal8",
211  }
212  bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
213  return parseSectionSwitch("__TEXT","__literal16",
215  }
216  bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
217  return parseSectionSwitch("__TEXT","__constructor");
218  }
219  bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
220  return parseSectionSwitch("__TEXT","__destructor");
221  }
222  bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
223  return parseSectionSwitch("__TEXT","__fvmlib_init0");
224  }
225  bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
226  return parseSectionSwitch("__TEXT","__fvmlib_init1");
227  }
228  bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
229  return parseSectionSwitch("__TEXT","__symbol_stub",
232  // FIXME: Different on PPC and ARM.
233  0, 16);
234  }
235  bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
236  return parseSectionSwitch("__TEXT","__picsymbol_stub",
239  }
240  bool parseSectionDirectiveData(StringRef, SMLoc) {
241  return parseSectionSwitch("__DATA", "__data");
242  }
243  bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
244  return parseSectionSwitch("__DATA", "__static_data");
245  }
246  bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
247  return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
249  }
250  bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
251  return parseSectionSwitch("__DATA", "__la_symbol_ptr",
253  }
254  bool parseSectionDirectiveDyld(StringRef, SMLoc) {
255  return parseSectionSwitch("__DATA", "__dyld");
256  }
257  bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
258  return parseSectionSwitch("__DATA", "__mod_init_func",
260  }
261  bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
262  return parseSectionSwitch("__DATA", "__mod_term_func",
264  }
265  bool parseSectionDirectiveConstData(StringRef, SMLoc) {
266  return parseSectionSwitch("__DATA", "__const");
267  }
268  bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
269  return parseSectionSwitch("__OBJC", "__class",
271  }
272  bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
273  return parseSectionSwitch("__OBJC", "__meta_class",
275  }
276  bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
277  return parseSectionSwitch("__OBJC", "__cat_cls_meth",
279  }
280  bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
281  return parseSectionSwitch("__OBJC", "__cat_inst_meth",
283  }
284  bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
285  return parseSectionSwitch("__OBJC", "__protocol",
287  }
288  bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
289  return parseSectionSwitch("__OBJC", "__string_object",
291  }
292  bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
293  return parseSectionSwitch("__OBJC", "__cls_meth",
295  }
296  bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
297  return parseSectionSwitch("__OBJC", "__inst_meth",
299  }
300  bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
301  return parseSectionSwitch("__OBJC", "__cls_refs",
304  }
305  bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
306  return parseSectionSwitch("__OBJC", "__message_refs",
309  }
310  bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
311  return parseSectionSwitch("__OBJC", "__symbols",
313  }
314  bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
315  return parseSectionSwitch("__OBJC", "__category",
317  }
318  bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
319  return parseSectionSwitch("__OBJC", "__class_vars",
321  }
322  bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
323  return parseSectionSwitch("__OBJC", "__instance_vars",
325  }
326  bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
327  return parseSectionSwitch("__OBJC", "__module_info",
329  }
330  bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
331  return parseSectionSwitch("__TEXT", "__cstring",
333  }
334  bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
335  return parseSectionSwitch("__TEXT", "__cstring",
337  }
338  bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
339  return parseSectionSwitch("__TEXT", "__cstring",
341  }
342  bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
343  return parseSectionSwitch("__OBJC", "__selector_strs",
345  }
346  bool parseSectionDirectiveTData(StringRef, SMLoc) {
347  return parseSectionSwitch("__DATA", "__thread_data",
349  }
350  bool parseSectionDirectiveText(StringRef, SMLoc) {
351  return parseSectionSwitch("__TEXT", "__text",
353  }
354  bool parseSectionDirectiveTLV(StringRef, SMLoc) {
355  return parseSectionSwitch("__DATA", "__thread_vars",
357  }
358  bool parseSectionDirectiveIdent(StringRef, SMLoc) {
359  // Darwin silently ignores the .ident directive.
360  getParser().eatToEndOfStatement();
361  return false;
362  }
363  bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
364  return parseSectionSwitch("__DATA", "__thread_init",
366  }
367  bool parseVersionMin(StringRef, SMLoc);
368 
369 };
370 
371 } // end anonymous namespace
372 
373 bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
374  const char *Section,
375  unsigned TAA, unsigned Align,
376  unsigned StubSize) {
377  if (getLexer().isNot(AsmToken::EndOfStatement))
378  return TokError("unexpected token in section switching directive");
379  Lex();
380 
381  // FIXME: Arch specific.
382  bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
383  getStreamer().SwitchSection(getContext().getMachOSection(
384  Segment, Section, TAA, StubSize,
385  isText ? SectionKind::getText()
387 
388  // Set the implicit alignment, if any.
389  //
390  // FIXME: This isn't really what 'as' does; I think it just uses the implicit
391  // alignment on the section (e.g., if one manually inserts bytes into the
392  // section, then just issuing the section switch directive will not realign
393  // the section. However, this is arguably more reasonable behavior, and there
394  // is no good reason for someone to intentionally emit incorrectly sized
395  // values into the implicitly aligned sections.
396  if (Align)
397  getStreamer().EmitValueToAlignment(Align);
398 
399  return false;
400 }
401 
402 /// parseDirectiveDesc
403 /// ::= .desc identifier , expression
404 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
405  StringRef Name;
406  if (getParser().parseIdentifier(Name))
407  return TokError("expected identifier in directive");
408 
409  // Handle the identifier as the key symbol.
410  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
411 
412  if (getLexer().isNot(AsmToken::Comma))
413  return TokError("unexpected token in '.desc' directive");
414  Lex();
415 
416  int64_t DescValue;
417  if (getParser().parseAbsoluteExpression(DescValue))
418  return true;
419 
420  if (getLexer().isNot(AsmToken::EndOfStatement))
421  return TokError("unexpected token in '.desc' directive");
422 
423  Lex();
424 
425  // Set the n_desc field of this Symbol to this DescValue
426  getStreamer().EmitSymbolDesc(Sym, DescValue);
427 
428  return false;
429 }
430 
431 /// parseDirectiveIndirectSymbol
432 /// ::= .indirect_symbol identifier
433 bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
434  const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
435  getStreamer().getCurrentSection().first);
437  if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
438  SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
439  SectionType != MachO::S_SYMBOL_STUBS)
440  return Error(Loc, "indirect symbol not in a symbol pointer or stub "
441  "section");
442 
443  StringRef Name;
444  if (getParser().parseIdentifier(Name))
445  return TokError("expected identifier in .indirect_symbol directive");
446 
447  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
448 
449  // Assembler local symbols don't make any sense here. Complain loudly.
450  if (Sym->isTemporary())
451  return TokError("non-local symbol required in directive");
452 
453  if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
454  return TokError("unable to emit indirect symbol attribute for: " + Name);
455 
456  if (getLexer().isNot(AsmToken::EndOfStatement))
457  return TokError("unexpected token in '.indirect_symbol' directive");
458 
459  Lex();
460 
461  return false;
462 }
463 
464 /// parseDirectiveDumpOrLoad
465 /// ::= ( .dump | .load ) "filename"
466 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
467  SMLoc IDLoc) {
468  bool IsDump = Directive == ".dump";
469  if (getLexer().isNot(AsmToken::String))
470  return TokError("expected string in '.dump' or '.load' directive");
471 
472  Lex();
473 
474  if (getLexer().isNot(AsmToken::EndOfStatement))
475  return TokError("unexpected token in '.dump' or '.load' directive");
476 
477  Lex();
478 
479  // FIXME: If/when .dump and .load are implemented they will be done in the
480  // the assembly parser and not have any need for an MCStreamer API.
481  if (IsDump)
482  return Warning(IDLoc, "ignoring directive .dump for now");
483  else
484  return Warning(IDLoc, "ignoring directive .load for now");
485 }
486 
487 /// ParseDirectiveLinkerOption
488 /// ::= .linker_option "string" ( , "string" )*
489 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
491  for (;;) {
492  if (getLexer().isNot(AsmToken::String))
493  return TokError("expected string in '" + Twine(IDVal) + "' directive");
494 
495  std::string Data;
496  if (getParser().parseEscapedString(Data))
497  return true;
498 
499  Args.push_back(Data);
500 
501  Lex();
502  if (getLexer().is(AsmToken::EndOfStatement))
503  break;
504 
505  if (getLexer().isNot(AsmToken::Comma))
506  return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
507  Lex();
508  }
509 
510  getStreamer().EmitLinkerOptions(Args);
511  return false;
512 }
513 
514 /// parseDirectiveLsym
515 /// ::= .lsym identifier , expression
516 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
517  StringRef Name;
518  if (getParser().parseIdentifier(Name))
519  return TokError("expected identifier in directive");
520 
521  // Handle the identifier as the key symbol.
522  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
523 
524  if (getLexer().isNot(AsmToken::Comma))
525  return TokError("unexpected token in '.lsym' directive");
526  Lex();
527 
528  const MCExpr *Value;
529  if (getParser().parseExpression(Value))
530  return true;
531 
532  if (getLexer().isNot(AsmToken::EndOfStatement))
533  return TokError("unexpected token in '.lsym' directive");
534 
535  Lex();
536 
537  // We don't currently support this directive.
538  //
539  // FIXME: Diagnostic location!
540  (void) Sym;
541  return TokError("directive '.lsym' is unsupported");
542 }
543 
544 /// parseDirectiveSection:
545 /// ::= .section identifier (',' identifier)*
546 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
547  SMLoc Loc = getLexer().getLoc();
548 
549  StringRef SectionName;
550  if (getParser().parseIdentifier(SectionName))
551  return Error(Loc, "expected identifier after '.section' directive");
552 
553  // Verify there is a following comma.
554  if (!getLexer().is(AsmToken::Comma))
555  return TokError("unexpected token in '.section' directive");
556 
557  std::string SectionSpec = SectionName;
558  SectionSpec += ",";
559 
560  // Add all the tokens until the end of the line, ParseSectionSpecifier will
561  // handle this.
562  StringRef EOL = getLexer().LexUntilEndOfStatement();
563  SectionSpec.append(EOL.begin(), EOL.end());
564 
565  Lex();
566  if (getLexer().isNot(AsmToken::EndOfStatement))
567  return TokError("unexpected token in '.section' directive");
568  Lex();
569 
570 
571  StringRef Segment, Section;
572  unsigned StubSize;
573  unsigned TAA;
574  bool TAAParsed;
575  std::string ErrorStr =
576  MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
577  TAA, TAAParsed, StubSize);
578 
579  if (!ErrorStr.empty())
580  return Error(Loc, ErrorStr.c_str());
581 
582  // FIXME: Arch specific.
583  bool isText = Segment == "__TEXT"; // FIXME: Hack.
584  getStreamer().SwitchSection(getContext().getMachOSection(
585  Segment, Section, TAA, StubSize,
586  isText ? SectionKind::getText()
588  return false;
589 }
590 
591 /// ParseDirectivePushSection:
592 /// ::= .pushsection identifier (',' identifier)*
593 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
594  getStreamer().PushSection();
595 
596  if (parseDirectiveSection(S, Loc)) {
597  getStreamer().PopSection();
598  return true;
599  }
600 
601  return false;
602 }
603 
604 /// ParseDirectivePopSection:
605 /// ::= .popsection
606 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
607  if (!getStreamer().PopSection())
608  return TokError(".popsection without corresponding .pushsection");
609  return false;
610 }
611 
612 /// ParseDirectivePrevious:
613 /// ::= .previous
614 bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
615  MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
616  if (!PreviousSection.first)
617  return TokError(".previous without corresponding .section");
618  getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
619  return false;
620 }
621 
622 /// ParseDirectiveSecureLogUnique
623 /// ::= .secure_log_unique ... message ...
624 bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
625  StringRef LogMessage = getParser().parseStringToEndOfStatement();
626  if (getLexer().isNot(AsmToken::EndOfStatement))
627  return TokError("unexpected token in '.secure_log_unique' directive");
628 
629  if (getContext().getSecureLogUsed())
630  return Error(IDLoc, ".secure_log_unique specified multiple times");
631 
632  // Get the secure log path.
633  const char *SecureLogFile = getContext().getSecureLogFile();
634  if (!SecureLogFile)
635  return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
636  "environment variable unset.");
637 
638  // Open the secure log file if we haven't already.
639  raw_ostream *OS = getContext().getSecureLog();
640  if (!OS) {
641  std::error_code EC;
642  OS = new raw_fd_ostream(SecureLogFile, EC,
644  if (EC) {
645  delete OS;
646  return Error(IDLoc, Twine("can't open secure log file: ") +
647  SecureLogFile + " (" + EC.message() + ")");
648  }
649  getContext().setSecureLog(OS);
650  }
651 
652  // Write the message.
653  unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
654  *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
655  << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
656  << LogMessage + "\n";
657 
658  getContext().setSecureLogUsed(true);
659 
660  return false;
661 }
662 
663 /// ParseDirectiveSecureLogReset
664 /// ::= .secure_log_reset
665 bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
666  if (getLexer().isNot(AsmToken::EndOfStatement))
667  return TokError("unexpected token in '.secure_log_reset' directive");
668 
669  Lex();
670 
671  getContext().setSecureLogUsed(false);
672 
673  return false;
674 }
675 
676 /// parseDirectiveSubsectionsViaSymbols
677 /// ::= .subsections_via_symbols
678 bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
679  if (getLexer().isNot(AsmToken::EndOfStatement))
680  return TokError("unexpected token in '.subsections_via_symbols' directive");
681 
682  Lex();
683 
684  getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
685 
686  return false;
687 }
688 
689 /// ParseDirectiveTBSS
690 /// ::= .tbss identifier, size, align
691 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
692  SMLoc IDLoc = getLexer().getLoc();
693  StringRef Name;
694  if (getParser().parseIdentifier(Name))
695  return TokError("expected identifier in directive");
696 
697  // Handle the identifier as the key symbol.
698  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
699 
700  if (getLexer().isNot(AsmToken::Comma))
701  return TokError("unexpected token in directive");
702  Lex();
703 
704  int64_t Size;
705  SMLoc SizeLoc = getLexer().getLoc();
706  if (getParser().parseAbsoluteExpression(Size))
707  return true;
708 
709  int64_t Pow2Alignment = 0;
710  SMLoc Pow2AlignmentLoc;
711  if (getLexer().is(AsmToken::Comma)) {
712  Lex();
713  Pow2AlignmentLoc = getLexer().getLoc();
714  if (getParser().parseAbsoluteExpression(Pow2Alignment))
715  return true;
716  }
717 
718  if (getLexer().isNot(AsmToken::EndOfStatement))
719  return TokError("unexpected token in '.tbss' directive");
720 
721  Lex();
722 
723  if (Size < 0)
724  return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
725  "zero");
726 
727  // FIXME: Diagnose overflow.
728  if (Pow2Alignment < 0)
729  return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
730  "than zero");
731 
732  if (!Sym->isUndefined())
733  return Error(IDLoc, "invalid symbol redefinition");
734 
735  getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
736  "__DATA", "__thread_bss",
739  Sym, Size, 1 << Pow2Alignment);
740 
741  return false;
742 }
743 
744 /// ParseDirectiveZerofill
745 /// ::= .zerofill segname , sectname [, identifier , size_expression [
746 /// , align_expression ]]
747 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
748  StringRef Segment;
749  if (getParser().parseIdentifier(Segment))
750  return TokError("expected segment name after '.zerofill' directive");
751 
752  if (getLexer().isNot(AsmToken::Comma))
753  return TokError("unexpected token in directive");
754  Lex();
755 
757  if (getParser().parseIdentifier(Section))
758  return TokError("expected section name after comma in '.zerofill' "
759  "directive");
760 
761  // If this is the end of the line all that was wanted was to create the
762  // the section but with no symbol.
763  if (getLexer().is(AsmToken::EndOfStatement)) {
764  // Create the zerofill section but no symbol
765  getStreamer().EmitZerofill(getContext().getMachOSection(
766  Segment, Section, MachO::S_ZEROFILL,
767  0, SectionKind::getBSS()));
768  return false;
769  }
770 
771  if (getLexer().isNot(AsmToken::Comma))
772  return TokError("unexpected token in directive");
773  Lex();
774 
775  SMLoc IDLoc = getLexer().getLoc();
776  StringRef IDStr;
777  if (getParser().parseIdentifier(IDStr))
778  return TokError("expected identifier in directive");
779 
780  // handle the identifier as the key symbol.
781  MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
782 
783  if (getLexer().isNot(AsmToken::Comma))
784  return TokError("unexpected token in directive");
785  Lex();
786 
787  int64_t Size;
788  SMLoc SizeLoc = getLexer().getLoc();
789  if (getParser().parseAbsoluteExpression(Size))
790  return true;
791 
792  int64_t Pow2Alignment = 0;
793  SMLoc Pow2AlignmentLoc;
794  if (getLexer().is(AsmToken::Comma)) {
795  Lex();
796  Pow2AlignmentLoc = getLexer().getLoc();
797  if (getParser().parseAbsoluteExpression(Pow2Alignment))
798  return true;
799  }
800 
801  if (getLexer().isNot(AsmToken::EndOfStatement))
802  return TokError("unexpected token in '.zerofill' directive");
803 
804  Lex();
805 
806  if (Size < 0)
807  return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
808  "than zero");
809 
810  // NOTE: The alignment in the directive is a power of 2 value, the assembler
811  // may internally end up wanting an alignment in bytes.
812  // FIXME: Diagnose overflow.
813  if (Pow2Alignment < 0)
814  return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
815  "can't be less than zero");
816 
817  if (!Sym->isUndefined())
818  return Error(IDLoc, "invalid symbol redefinition");
819 
820  // Create the zerofill Symbol with Size and Pow2Alignment
821  //
822  // FIXME: Arch specific.
823  getStreamer().EmitZerofill(getContext().getMachOSection(
824  Segment, Section, MachO::S_ZEROFILL,
825  0, SectionKind::getBSS()),
826  Sym, Size, 1 << Pow2Alignment);
827 
828  return false;
829 }
830 
831 /// ParseDirectiveDataRegion
832 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
833 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
834  if (getLexer().is(AsmToken::EndOfStatement)) {
835  Lex();
836  getStreamer().EmitDataRegion(MCDR_DataRegion);
837  return false;
838  }
839  StringRef RegionType;
840  SMLoc Loc = getParser().getTok().getLoc();
841  if (getParser().parseIdentifier(RegionType))
842  return TokError("expected region type after '.data_region' directive");
843  int Kind = StringSwitch<int>(RegionType)
844  .Case("jt8", MCDR_DataRegionJT8)
845  .Case("jt16", MCDR_DataRegionJT16)
846  .Case("jt32", MCDR_DataRegionJT32)
847  .Default(-1);
848  if (Kind == -1)
849  return Error(Loc, "unknown region type in '.data_region' directive");
850  Lex();
851 
852  getStreamer().EmitDataRegion((MCDataRegionType)Kind);
853  return false;
854 }
855 
856 /// ParseDirectiveDataRegionEnd
857 /// ::= .end_data_region
858 bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
859  if (getLexer().isNot(AsmToken::EndOfStatement))
860  return TokError("unexpected token in '.end_data_region' directive");
861 
862  Lex();
863  getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
864  return false;
865 }
866 
867 /// parseVersionMin
868 /// ::= .ios_version_min major,minor[,update]
869 /// ::= .macosx_version_min major,minor[,update]
870 bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc) {
871  int64_t Major = 0, Minor = 0, Update = 0;
872  int Kind = StringSwitch<int>(Directive)
873  .Case(".ios_version_min", MCVM_IOSVersionMin)
874  .Case(".macosx_version_min", MCVM_OSXVersionMin);
875  // Get the major version number.
876  if (getLexer().isNot(AsmToken::Integer))
877  return TokError("invalid OS major version number");
878  Major = getLexer().getTok().getIntVal();
879  if (Major > 65535 || Major <= 0)
880  return TokError("invalid OS major version number");
881  Lex();
882  if (getLexer().isNot(AsmToken::Comma))
883  return TokError("minor OS version number required, comma expected");
884  Lex();
885  // Get the minor version number.
886  if (getLexer().isNot(AsmToken::Integer))
887  return TokError("invalid OS minor version number");
888  Minor = getLexer().getTok().getIntVal();
889  if (Minor > 255 || Minor < 0)
890  return TokError("invalid OS minor version number");
891  Lex();
892  // Get the update level, if specified
893  if (getLexer().isNot(AsmToken::EndOfStatement)) {
894  if (getLexer().isNot(AsmToken::Comma))
895  return TokError("invalid update specifier, comma expected");
896  Lex();
897  if (getLexer().isNot(AsmToken::Integer))
898  return TokError("invalid OS update number");
899  Update = getLexer().getTok().getIntVal();
900  if (Update > 255 || Update < 0)
901  return TokError("invalid OS update number");
902  Lex();
903  }
904 
905  // We've parsed a correct version specifier, so send it to the streamer.
906  getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
907 
908  return false;
909 }
910 
911 namespace llvm {
912 
914  return new DarwinAsmParser;
915 }
916 
917 } // end llvm namespace
void push_back(const T &Elt)
Definition: SmallVector.h:222
F_Append - When opening a file, if it already exists append to the existing file instead of returning...
Definition: FileSystem.h:588
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
MCSectionMachO - This represents a section on a Mach-O system (used by Mac OS X). ...
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:64
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
static SectionKind getDataRel()
Definition: SectionKind.h:227
.ios_version_min
Definition: MCDirectives.h:64
MCAsmParserExtension * createDarwinAsmParser()
S_THREAD_LOCAL_VARIABLES - Section with thread local variable structure data.
.macosx_version_min
Definition: MCDirectives.h:65
MCDataRegionType
Definition: MCDirectives.h:55
StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:55
S_4BYTE_LITERALS - Section with 4 byte literals.
static SectionKind getBSS()
Definition: SectionKind.h:223
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
S_8BYTE_LITERALS - Section with 8 byte literals.
.data_region jt16
Definition: MCDirectives.h:58
S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for termination.
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:44
iterator begin() const
Definition: StringRef.h:90
.data_region jt32
Definition: MCDirectives.h:59
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
S_LITERAL_POINTERS - Section with pointers to literals.
static std::string ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:49
S_THREAD_LOCAL_REGULAR - Thread local data section.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:68
S_CSTRING_LITERALS - Section with literal C strings.
S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
S_ATTR_NO_DEAD_STRIP - No dead stripping.
static SectionKind getThreadBSS()
Definition: SectionKind.h:221
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
MachO::SectionType getType() const
S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local variable initialization pointers to...
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
R Default(const T &Value) const
Definition: StringSwitch.h:111
S_16BYTE_LITERALS - Section with only 16 byte literals.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:222
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:592
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
const ARM::ArchExtKind Kind
.data_region jt8
Definition: MCDirectives.h:57
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
MCVersionMinType
Definition: MCDirectives.h:63
LLVM Value Representation.
Definition: Value.h:69
iterator end() const
Definition: StringRef.h:92
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:258
S_ZEROFILL - Zero fill on demand section.
Represents a location in source code.
Definition: SMLoc.h:23
S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for initialization.
SectionType
These are the section type and attributes fields.
.end_data_region
Definition: MCDirectives.h:60
static SectionKind getText()
Definition: SectionKind.h:207