DarwinAsmParser.cpp 34 KB

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