blob: 73e068a34391107b44f0625a24d635683ad66145 [file] [log] [blame]
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +00001//===- 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/MC/MCParser/MCAsmParserExtension.h"
Rafael Espindolac49ac5e2015-12-16 23:49:14 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/StringSwitch.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000014#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Twine.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000016#include "llvm/MC/MCContext.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000017#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000020#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSymbol.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000023#include "llvm/Support/FileSystem.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/SourceMgr.h"
26using namespace llvm;
27
28namespace {
29
30/// \brief Implementation of directive handling which is shared across all
31/// Darwin targets.
32class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000033 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000034 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000035 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
36 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000037 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000038 }
39
Jim Grosbach319026d2014-03-18 22:09:10 +000040 bool parseSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000041 unsigned TAA = 0, unsigned ImplicitAlign = 0,
42 unsigned StubSize = 0);
43
Tim Northover2d4d1612015-10-28 22:36:05 +000044 SMLoc LastVersionMinDirective;
45
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000046public:
47 DarwinAsmParser() {}
48
Craig Topper59be68f2014-03-08 07:14:16 +000049 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000050 // Call the base implementation.
51 this->MCAsmParserExtension::Initialize(Parser);
52
Jim Grosbach319026d2014-03-18 22:09:10 +000053 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
54 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
Kevin Enderby3aeada22013-08-28 17:50:59 +000055 ".indirect_symbol");
Jim Grosbach319026d2014-03-18 22:09:10 +000056 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
57 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000058 ".subsections_via_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +000059 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
60 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
61 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
62 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000063 ".pushsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000064 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000065 ".popsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000066 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
67 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000068 ".secure_log_unique");
Jim Grosbach319026d2014-03-18 22:09:10 +000069 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000070 ".secure_log_reset");
Jim Grosbach319026d2014-03-18 22:09:10 +000071 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
72 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000073
Jim Grosbach319026d2014-03-18 22:09:10 +000074 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000075 ".data_region");
Jim Grosbach319026d2014-03-18 22:09:10 +000076 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000077 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000078
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000079 // Special section directives.
Jim Grosbach319026d2014-03-18 22:09:10 +000080 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
81 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
82 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000083 ".const_data");
Jim Grosbach319026d2014-03-18 22:09:10 +000084 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000085 ".constructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000086 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000087 ".cstring");
Jim Grosbach319026d2014-03-18 22:09:10 +000088 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
89 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000090 ".destructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000091 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
92 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000093 ".fvmlib_init0");
Jim Grosbach319026d2014-03-18 22:09:10 +000094 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000095 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000096 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +000097 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000098 ".lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +000099 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +0000100 ".linker_option");
Jim Grosbach319026d2014-03-18 22:09:10 +0000101 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000102 ".literal16");
Jim Grosbach319026d2014-03-18 22:09:10 +0000103 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000104 ".literal4");
Jim Grosbach319026d2014-03-18 22:09:10 +0000105 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000106 ".literal8");
Jim Grosbach319026d2014-03-18 22:09:10 +0000107 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000108 ".mod_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000109 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000110 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000111 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000112 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000113 ".non_lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000114 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000115 ".objc_cat_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000116 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000117 ".objc_cat_inst_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000118 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000119 ".objc_category");
Jim Grosbach319026d2014-03-18 22:09:10 +0000120 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000121 ".objc_class");
Jim Grosbach319026d2014-03-18 22:09:10 +0000122 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000123 ".objc_class_names");
Jim Grosbach319026d2014-03-18 22:09:10 +0000124 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000125 ".objc_class_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000126 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000127 ".objc_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000128 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000129 ".objc_cls_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000130 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000131 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000132 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000133 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000134 ".objc_instance_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000135 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000136 ".objc_message_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000137 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000138 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000139 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000140 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000141 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000142 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000143 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000144 ".objc_meth_var_types");
Jim Grosbach319026d2014-03-18 22:09:10 +0000145 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000146 ".objc_module_info");
Jim Grosbach319026d2014-03-18 22:09:10 +0000147 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000148 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000149 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000150 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000151 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000152 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000153 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000154 ".objc_string_object");
Jim Grosbach319026d2014-03-18 22:09:10 +0000155 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000156 ".objc_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +0000157 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000158 ".picsymbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000159 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000160 ".static_const");
Jim Grosbach319026d2014-03-18 22:09:10 +0000161 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000162 ".static_data");
Jim Grosbach319026d2014-03-18 22:09:10 +0000163 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000164 ".symbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000165 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
167 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000168 ".thread_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000169 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000170
Jim Grosbach319026d2014-03-18 22:09:10 +0000171 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
Tim Northover2d4d1612015-10-28 22:36:05 +0000172 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
173 ".watchos_version_min");
174 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
Jim Grosbach319026d2014-03-18 22:09:10 +0000175 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
176 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
Jim Grosbach448334a2014-03-18 22:09:05 +0000177 ".macosx_version_min");
Tim Northover2d4d1612015-10-28 22:36:05 +0000178
179 LastVersionMinDirective = SMLoc();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000180 }
181
Jim Grosbach319026d2014-03-18 22:09:10 +0000182 bool parseDirectiveDesc(StringRef, SMLoc);
183 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
184 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
185 bool parseDirectiveLsym(StringRef, SMLoc);
186 bool parseDirectiveLinkerOption(StringRef, SMLoc);
187 bool parseDirectiveSection(StringRef, SMLoc);
188 bool parseDirectivePushSection(StringRef, SMLoc);
189 bool parseDirectivePopSection(StringRef, SMLoc);
190 bool parseDirectivePrevious(StringRef, SMLoc);
191 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
192 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
193 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
194 bool parseDirectiveTBSS(StringRef, SMLoc);
195 bool parseDirectiveZerofill(StringRef, SMLoc);
196 bool parseDirectiveDataRegion(StringRef, SMLoc);
197 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000198
199 // Named Section Directive
Jim Grosbach319026d2014-03-18 22:09:10 +0000200 bool parseSectionDirectiveBss(StringRef, SMLoc) {
201 return parseSectionSwitch("__DATA", "__bss");
Rafael Espindola3402c052013-10-02 14:09:29 +0000202 }
203
Jim Grosbach319026d2014-03-18 22:09:10 +0000204 bool parseSectionDirectiveConst(StringRef, SMLoc) {
205 return parseSectionSwitch("__TEXT", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000206 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000207 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
208 return parseSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000209 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000210 bool parseSectionDirectiveCString(StringRef, SMLoc) {
211 return parseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000212 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000213 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000214 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
215 return parseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000216 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000217 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000218 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
219 return parseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000220 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000221 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000222 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
223 return parseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000224 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000225 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000226 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
227 return parseSectionSwitch("__TEXT","__constructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000228 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000229 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
230 return parseSectionSwitch("__TEXT","__destructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000231 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000232 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
233 return parseSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000234 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000235 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
236 return parseSectionSwitch("__TEXT","__fvmlib_init1");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000237 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000238 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
239 return parseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000240 MachO::S_SYMBOL_STUBS |
241 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000242 // FIXME: Different on PPC and ARM.
243 0, 16);
244 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000245 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
246 return parseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000247 MachO::S_SYMBOL_STUBS |
248 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000249 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000250 bool parseSectionDirectiveData(StringRef, SMLoc) {
251 return parseSectionSwitch("__DATA", "__data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000252 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000253 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
254 return parseSectionSwitch("__DATA", "__static_data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000255 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000256 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
257 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000258 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000259 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000260 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
261 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000262 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000263 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000264 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
265 return parseSectionSwitch("__DATA", "__dyld");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000266 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000267 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
268 return parseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000269 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000270 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000271 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
272 return parseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000273 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000274 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000275 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
276 return parseSectionSwitch("__DATA", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000277 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000278 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
279 return parseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000280 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000281 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000282 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
283 return parseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000284 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000285 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000286 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
287 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000288 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000289 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000290 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
291 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000292 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000293 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000294 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
295 return parseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000296 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000297 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000298 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
299 return parseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000300 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000301 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000302 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
303 return parseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000304 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000305 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000306 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
307 return parseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000308 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000309 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000310 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
311 return parseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000312 MachO::S_ATTR_NO_DEAD_STRIP |
313 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000314 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000315 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
316 return parseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000317 MachO::S_ATTR_NO_DEAD_STRIP |
318 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000319 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000320 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
321 return parseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000322 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000323 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000324 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
325 return parseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000326 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000327 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000328 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
329 return parseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000330 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000331 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000332 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
333 return parseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000334 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000335 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000336 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
337 return parseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000338 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000339 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000340 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
341 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000342 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000343 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000344 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
345 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000346 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000347 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000348 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
349 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000350 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000351 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000352 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
353 return parseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000354 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000355 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000356 bool parseSectionDirectiveTData(StringRef, SMLoc) {
357 return parseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000358 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000359 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000360 bool parseSectionDirectiveText(StringRef, SMLoc) {
361 return parseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000362 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000363 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000364 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
365 return parseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000366 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000367 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000368 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
Jim Grosbach14be61a2011-03-08 19:17:19 +0000369 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000370 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000371 return false;
372 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000373 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
374 return parseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000375 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000376 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000377 bool parseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000378
379};
380
Bill Wendlingce4fe412012-08-08 06:30:30 +0000381} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000382
Jim Grosbach319026d2014-03-18 22:09:10 +0000383bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000384 const char *Section,
385 unsigned TAA, unsigned Align,
386 unsigned StubSize) {
387 if (getLexer().isNot(AsmToken::EndOfStatement))
388 return TokError("unexpected token in section switching directive");
389 Lex();
390
391 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000392 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000393 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000394 Segment, Section, TAA, StubSize,
395 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000396
397 // Set the implicit alignment, if any.
398 //
399 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
400 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000401 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000402 // the section. However, this is arguably more reasonable behavior, and there
403 // is no good reason for someone to intentionally emit incorrectly sized
404 // values into the implicitly aligned sections.
405 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000406 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000407
408 return false;
409}
410
Jim Grosbach319026d2014-03-18 22:09:10 +0000411/// parseDirectiveDesc
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000412/// ::= .desc identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000413bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000414 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000415 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000416 return TokError("expected identifier in directive");
417
418 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000419 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000420
421 if (getLexer().isNot(AsmToken::Comma))
422 return TokError("unexpected token in '.desc' directive");
423 Lex();
424
425 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000426 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000427 return true;
428
429 if (getLexer().isNot(AsmToken::EndOfStatement))
430 return TokError("unexpected token in '.desc' directive");
431
432 Lex();
433
434 // Set the n_desc field of this Symbol to this DescValue
435 getStreamer().EmitSymbolDesc(Sym, DescValue);
436
437 return false;
438}
439
Jim Grosbach319026d2014-03-18 22:09:10 +0000440/// parseDirectiveIndirectSymbol
Kevin Enderby3aeada22013-08-28 17:50:59 +0000441/// ::= .indirect_symbol identifier
Jim Grosbach319026d2014-03-18 22:09:10 +0000442bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
Kevin Enderby3aeada22013-08-28 17:50:59 +0000443 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
444 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000445 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000446 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
447 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
448 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000449 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
450 "section");
451
452 StringRef Name;
453 if (getParser().parseIdentifier(Name))
454 return TokError("expected identifier in .indirect_symbol directive");
455
Jim Grosbach6f482002015-05-18 18:43:14 +0000456 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000457
458 // Assembler local symbols don't make any sense here. Complain loudly.
459 if (Sym->isTemporary())
460 return TokError("non-local symbol required in directive");
461
462 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
463 return TokError("unable to emit indirect symbol attribute for: " + Name);
464
465 if (getLexer().isNot(AsmToken::EndOfStatement))
466 return TokError("unexpected token in '.indirect_symbol' directive");
467
468 Lex();
469
470 return false;
471}
472
Jim Grosbach319026d2014-03-18 22:09:10 +0000473/// parseDirectiveDumpOrLoad
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000474/// ::= ( .dump | .load ) "filename"
Jim Grosbach319026d2014-03-18 22:09:10 +0000475bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000476 SMLoc IDLoc) {
477 bool IsDump = Directive == ".dump";
478 if (getLexer().isNot(AsmToken::String))
479 return TokError("expected string in '.dump' or '.load' directive");
480
481 Lex();
482
483 if (getLexer().isNot(AsmToken::EndOfStatement))
484 return TokError("unexpected token in '.dump' or '.load' directive");
485
486 Lex();
487
488 // FIXME: If/when .dump and .load are implemented they will be done in the
489 // the assembly parser and not have any need for an MCStreamer API.
490 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000491 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000492 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000493 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000494}
495
Daniel Dunbar16004b82013-01-18 01:25:48 +0000496/// ParseDirectiveLinkerOption
497/// ::= .linker_option "string" ( , "string" )*
Jim Grosbach319026d2014-03-18 22:09:10 +0000498bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000499 SmallVector<std::string, 4> Args;
500 for (;;) {
501 if (getLexer().isNot(AsmToken::String))
502 return TokError("expected string in '" + Twine(IDVal) + "' directive");
503
504 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000505 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000506 return true;
507
508 Args.push_back(Data);
509
510 Lex();
511 if (getLexer().is(AsmToken::EndOfStatement))
512 break;
513
514 if (getLexer().isNot(AsmToken::Comma))
515 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
516 Lex();
517 }
518
519 getStreamer().EmitLinkerOptions(Args);
520 return false;
521}
522
Jim Grosbach319026d2014-03-18 22:09:10 +0000523/// parseDirectiveLsym
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000524/// ::= .lsym identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000525bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000526 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000527 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000528 return TokError("expected identifier in directive");
529
530 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000531 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000532
533 if (getLexer().isNot(AsmToken::Comma))
534 return TokError("unexpected token in '.lsym' directive");
535 Lex();
536
537 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000538 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000539 return true;
540
541 if (getLexer().isNot(AsmToken::EndOfStatement))
542 return TokError("unexpected token in '.lsym' directive");
543
544 Lex();
545
546 // We don't currently support this directive.
547 //
548 // FIXME: Diagnostic location!
549 (void) Sym;
550 return TokError("directive '.lsym' is unsupported");
551}
552
Jim Grosbach319026d2014-03-18 22:09:10 +0000553/// parseDirectiveSection:
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000554/// ::= .section identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000555bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000556 SMLoc Loc = getLexer().getLoc();
557
558 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000559 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000560 return Error(Loc, "expected identifier after '.section' directive");
561
562 // Verify there is a following comma.
563 if (!getLexer().is(AsmToken::Comma))
564 return TokError("unexpected token in '.section' directive");
565
566 std::string SectionSpec = SectionName;
567 SectionSpec += ",";
568
569 // Add all the tokens until the end of the line, ParseSectionSpecifier will
570 // handle this.
571 StringRef EOL = getLexer().LexUntilEndOfStatement();
572 SectionSpec.append(EOL.begin(), EOL.end());
573
574 Lex();
575 if (getLexer().isNot(AsmToken::EndOfStatement))
576 return TokError("unexpected token in '.section' directive");
577 Lex();
578
579
580 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000581 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000582 unsigned TAA;
583 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000584 std::string ErrorStr =
585 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000586 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000587
588 if (!ErrorStr.empty())
589 return Error(Loc, ErrorStr.c_str());
590
Akira Hatanaka8ad73992015-10-15 05:28:38 +0000591 // Issue a warning if the target is not powerpc and Section is a *coal* section.
592 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
593 Triple::ArchType ArchTy = TT.getArch();
594
595 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
596 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
597 .Case("__textcoal_nt", "__text")
598 .Case("__const_coal", "__const")
599 .Case("__datacoal_nt", "__data")
600 .Default(Section);
601
602 if (!Section.equals(NonCoalSection)) {
603 StringRef SectionVal(Loc.getPointer());
604 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
605 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
606 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
607 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
608 SMRange(BLoc, ELoc));
609 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
610 "\"", SMRange(BLoc, ELoc));
611 }
612 }
613
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000614 // FIXME: Arch specific.
615 bool isText = Segment == "__TEXT"; // FIXME: Hack.
616 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000617 Segment, Section, TAA, StubSize,
618 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000619 return false;
620}
621
Bill Wendlingce4fe412012-08-08 06:30:30 +0000622/// ParseDirectivePushSection:
623/// ::= .pushsection identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000624bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000625 getStreamer().PushSection();
626
Jim Grosbach319026d2014-03-18 22:09:10 +0000627 if (parseDirectiveSection(S, Loc)) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000628 getStreamer().PopSection();
629 return true;
630 }
631
632 return false;
633}
634
635/// ParseDirectivePopSection:
636/// ::= .popsection
Jim Grosbach319026d2014-03-18 22:09:10 +0000637bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000638 if (!getStreamer().PopSection())
639 return TokError(".popsection without corresponding .pushsection");
640 return false;
641}
642
643/// ParseDirectivePrevious:
644/// ::= .previous
Jim Grosbach319026d2014-03-18 22:09:10 +0000645bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000646 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000647 if (!PreviousSection.first)
648 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000649 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000650 return false;
651}
652
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000653/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000654/// ::= .secure_log_unique ... message ...
Jim Grosbach319026d2014-03-18 22:09:10 +0000655bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000656 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000657 if (getLexer().isNot(AsmToken::EndOfStatement))
658 return TokError("unexpected token in '.secure_log_unique' directive");
659
David Blaikiedc3f01e2015-03-09 01:57:13 +0000660 if (getContext().getSecureLogUsed())
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000661 return Error(IDLoc, ".secure_log_unique specified multiple times");
662
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000663 // Get the secure log path.
664 const char *SecureLogFile = getContext().getSecureLogFile();
Craig Topper353eda42014-04-24 06:44:33 +0000665 if (!SecureLogFile)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000666 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
667 "environment variable unset.");
668
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000669 // Open the secure log file if we haven't already.
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000670 raw_fd_ostream *OS = getContext().getSecureLog();
Craig Topper353eda42014-04-24 06:44:33 +0000671 if (!OS) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000672 std::error_code EC;
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000673 auto NewOS = llvm::make_unique<raw_fd_ostream>(
674 SecureLogFile, EC, sys::fs::F_Append | sys::fs::F_Text);
675 if (EC)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000676 return Error(IDLoc, Twine("can't open secure log file: ") +
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000677 SecureLogFile + " (" + EC.message() + ")");
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000678 OS = NewOS.get();
679 getContext().setSecureLog(std::move(NewOS));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000680 }
681
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000682 // Write the message.
Alp Tokera55b95b2014-07-06 10:33:31 +0000683 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000684 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
685 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
686 << LogMessage + "\n";
687
688 getContext().setSecureLogUsed(true);
689
690 return false;
691}
692
693/// ParseDirectiveSecureLogReset
694/// ::= .secure_log_reset
Jim Grosbach319026d2014-03-18 22:09:10 +0000695bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000696 if (getLexer().isNot(AsmToken::EndOfStatement))
697 return TokError("unexpected token in '.secure_log_reset' directive");
698
699 Lex();
700
701 getContext().setSecureLogUsed(false);
702
703 return false;
704}
705
Jim Grosbach319026d2014-03-18 22:09:10 +0000706/// parseDirectiveSubsectionsViaSymbols
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000707/// ::= .subsections_via_symbols
Jim Grosbach319026d2014-03-18 22:09:10 +0000708bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000709 if (getLexer().isNot(AsmToken::EndOfStatement))
710 return TokError("unexpected token in '.subsections_via_symbols' directive");
711
712 Lex();
713
714 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
715
716 return false;
717}
718
719/// ParseDirectiveTBSS
720/// ::= .tbss identifier, size, align
Jim Grosbach319026d2014-03-18 22:09:10 +0000721bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000722 SMLoc IDLoc = getLexer().getLoc();
723 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000724 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000725 return TokError("expected identifier in directive");
726
727 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000728 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000729
730 if (getLexer().isNot(AsmToken::Comma))
731 return TokError("unexpected token in directive");
732 Lex();
733
734 int64_t Size;
735 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000736 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000737 return true;
738
739 int64_t Pow2Alignment = 0;
740 SMLoc Pow2AlignmentLoc;
741 if (getLexer().is(AsmToken::Comma)) {
742 Lex();
743 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000744 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000745 return true;
746 }
747
748 if (getLexer().isNot(AsmToken::EndOfStatement))
749 return TokError("unexpected token in '.tbss' directive");
750
751 Lex();
752
753 if (Size < 0)
754 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
755 "zero");
756
757 // FIXME: Diagnose overflow.
758 if (Pow2Alignment < 0)
759 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
760 "than zero");
761
762 if (!Sym->isUndefined())
763 return Error(IDLoc, "invalid symbol redefinition");
764
765 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
766 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000767 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000768 0, SectionKind::getThreadBSS()),
769 Sym, Size, 1 << Pow2Alignment);
770
771 return false;
772}
773
774/// ParseDirectiveZerofill
775/// ::= .zerofill segname , sectname [, identifier , size_expression [
776/// , align_expression ]]
Jim Grosbach319026d2014-03-18 22:09:10 +0000777bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000778 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000779 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000780 return TokError("expected segment name after '.zerofill' directive");
781
782 if (getLexer().isNot(AsmToken::Comma))
783 return TokError("unexpected token in directive");
784 Lex();
785
786 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000787 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000788 return TokError("expected section name after comma in '.zerofill' "
789 "directive");
790
791 // If this is the end of the line all that was wanted was to create the
792 // the section but with no symbol.
793 if (getLexer().is(AsmToken::EndOfStatement)) {
794 // Create the zerofill section but no symbol
795 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000796 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000797 0, SectionKind::getBSS()));
798 return false;
799 }
800
801 if (getLexer().isNot(AsmToken::Comma))
802 return TokError("unexpected token in directive");
803 Lex();
804
805 SMLoc IDLoc = getLexer().getLoc();
806 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000807 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000808 return TokError("expected identifier in directive");
809
810 // handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000811 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000812
813 if (getLexer().isNot(AsmToken::Comma))
814 return TokError("unexpected token in directive");
815 Lex();
816
817 int64_t Size;
818 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000819 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000820 return true;
821
822 int64_t Pow2Alignment = 0;
823 SMLoc Pow2AlignmentLoc;
824 if (getLexer().is(AsmToken::Comma)) {
825 Lex();
826 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000827 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000828 return true;
829 }
830
831 if (getLexer().isNot(AsmToken::EndOfStatement))
832 return TokError("unexpected token in '.zerofill' directive");
833
834 Lex();
835
836 if (Size < 0)
837 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
838 "than zero");
839
840 // NOTE: The alignment in the directive is a power of 2 value, the assembler
841 // may internally end up wanting an alignment in bytes.
842 // FIXME: Diagnose overflow.
843 if (Pow2Alignment < 0)
844 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
845 "can't be less than zero");
846
847 if (!Sym->isUndefined())
848 return Error(IDLoc, "invalid symbol redefinition");
849
850 // Create the zerofill Symbol with Size and Pow2Alignment
851 //
852 // FIXME: Arch specific.
853 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000854 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000855 0, SectionKind::getBSS()),
856 Sym, Size, 1 << Pow2Alignment);
857
858 return false;
859}
860
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000861/// ParseDirectiveDataRegion
862/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
Jim Grosbach319026d2014-03-18 22:09:10 +0000863bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000864 if (getLexer().is(AsmToken::EndOfStatement)) {
865 Lex();
866 getStreamer().EmitDataRegion(MCDR_DataRegion);
867 return false;
868 }
869 StringRef RegionType;
870 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000871 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000872 return TokError("expected region type after '.data_region' directive");
873 int Kind = StringSwitch<int>(RegionType)
874 .Case("jt8", MCDR_DataRegionJT8)
875 .Case("jt16", MCDR_DataRegionJT16)
876 .Case("jt32", MCDR_DataRegionJT32)
877 .Default(-1);
878 if (Kind == -1)
879 return Error(Loc, "unknown region type in '.data_region' directive");
880 Lex();
881
882 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
883 return false;
884}
885
886/// ParseDirectiveDataRegionEnd
887/// ::= .end_data_region
Jim Grosbach319026d2014-03-18 22:09:10 +0000888bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000889 if (getLexer().isNot(AsmToken::EndOfStatement))
890 return TokError("unexpected token in '.end_data_region' directive");
891
892 Lex();
893 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
894 return false;
895}
896
Jim Grosbach319026d2014-03-18 22:09:10 +0000897/// parseVersionMin
Jim Grosbach448334a2014-03-18 22:09:05 +0000898/// ::= .ios_version_min major,minor[,update]
899/// ::= .macosx_version_min major,minor[,update]
Tim Northover2d4d1612015-10-28 22:36:05 +0000900bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
Jim Grosbach448334a2014-03-18 22:09:05 +0000901 int64_t Major = 0, Minor = 0, Update = 0;
902 int Kind = StringSwitch<int>(Directive)
Tim Northover2d4d1612015-10-28 22:36:05 +0000903 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
904 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
Jim Grosbach448334a2014-03-18 22:09:05 +0000905 .Case(".ios_version_min", MCVM_IOSVersionMin)
906 .Case(".macosx_version_min", MCVM_OSXVersionMin);
907 // Get the major version number.
908 if (getLexer().isNot(AsmToken::Integer))
909 return TokError("invalid OS major version number");
910 Major = getLexer().getTok().getIntVal();
911 if (Major > 65535 || Major <= 0)
912 return TokError("invalid OS major version number");
913 Lex();
914 if (getLexer().isNot(AsmToken::Comma))
915 return TokError("minor OS version number required, comma expected");
916 Lex();
917 // Get the minor version number.
918 if (getLexer().isNot(AsmToken::Integer))
919 return TokError("invalid OS minor version number");
920 Minor = getLexer().getTok().getIntVal();
921 if (Minor > 255 || Minor < 0)
922 return TokError("invalid OS minor version number");
923 Lex();
924 // Get the update level, if specified
925 if (getLexer().isNot(AsmToken::EndOfStatement)) {
926 if (getLexer().isNot(AsmToken::Comma))
927 return TokError("invalid update specifier, comma expected");
928 Lex();
929 if (getLexer().isNot(AsmToken::Integer))
930 return TokError("invalid OS update number");
931 Update = getLexer().getTok().getIntVal();
932 if (Update > 255 || Update < 0)
933 return TokError("invalid OS update number");
934 Lex();
935 }
936
Tim Northover2d4d1612015-10-28 22:36:05 +0000937 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
938 Triple::OSType ExpectedOS = Triple::UnknownOS;
939 switch ((MCVersionMinType)Kind) {
940 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
941 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
942 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
943 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
944 }
945 if (T.getOS() != ExpectedOS)
946 Warning(Loc, Directive + " should only be used for " +
947 Triple::getOSTypeName(ExpectedOS) + " targets");
948
949 if (LastVersionMinDirective.isValid()) {
950 Warning(Loc, "overriding previous version_min directive");
951 Note(LastVersionMinDirective, "previous definition is here");
952 }
953 LastVersionMinDirective = Loc;
954
Jim Grosbach448334a2014-03-18 22:09:05 +0000955 // We've parsed a correct version specifier, so send it to the streamer.
956 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
957
958 return false;
959}
960
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000961namespace llvm {
962
963MCAsmParserExtension *createDarwinAsmParser() {
964 return new DarwinAsmParser;
965}
966
Bill Wendlingce4fe412012-08-08 06:30:30 +0000967} // end llvm namespace