1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/MC/MCStreamer.h"
22 class ELFAsmParser : public MCAsmParserExtension {
23 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
29 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
39 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
49 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
56 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
57 // the best way for us to get access to it?
58 bool ParseSectionDirectiveData(StringRef, SMLoc) {
59 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
60 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61 SectionKind::getDataRel());
63 bool ParseSectionDirectiveText(StringRef, SMLoc) {
64 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_EXECINSTR |
66 MCSectionELF::SHF_ALLOC, SectionKind::getText());
68 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
69 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
70 MCSectionELF::SHF_WRITE |
71 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
73 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
74 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
75 MCSectionELF::SHF_ALLOC,
76 SectionKind::getReadOnly());
78 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
79 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
80 MCSectionELF::SHF_ALLOC |
81 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
82 SectionKind::getThreadData());
84 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
85 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
86 MCSectionELF::SHF_ALLOC |
87 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
88 SectionKind::getThreadBSS());
90 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
91 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
92 MCSectionELF::SHF_ALLOC |
93 MCSectionELF::SHF_WRITE,
94 SectionKind::getDataRel());
96 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
97 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
98 MCSectionELF::SHF_ALLOC |
99 MCSectionELF::SHF_WRITE,
100 SectionKind::getReadOnlyWithRel());
102 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
103 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
104 MCSectionELF::SHF_ALLOC |
105 MCSectionELF::SHF_WRITE,
106 SectionKind::getReadOnlyWithRelLocal());
108 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
109 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
110 MCSectionELF::SHF_ALLOC |
111 MCSectionELF::SHF_WRITE,
112 SectionKind::getDataRel());
114 bool ParseDirectiveSection(StringRef, SMLoc);
115 bool ParseDirectiveSize(StringRef, SMLoc);
116 bool ParseDirectivePrevious(StringRef, SMLoc);
117 bool ParseDirectiveType(StringRef, SMLoc);
118 bool ParseDirectiveIdent(StringRef, SMLoc);
121 bool ParseSectionName(StringRef &SectionName);
126 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
127 unsigned Flags, SectionKind Kind) {
128 if (getLexer().isNot(AsmToken::EndOfStatement))
129 return TokError("unexpected token in section switching directive");
132 getStreamer().SwitchSection(getContext().getELFSection(
133 Section, Type, Flags, Kind));
138 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
140 if (getParser().ParseIdentifier(Name))
141 return TokError("expected identifier in directive");
142 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
144 if (getLexer().isNot(AsmToken::Comma))
145 return TokError("unexpected token in directive");
149 if (getParser().ParseExpression(Expr))
152 if (getLexer().isNot(AsmToken::EndOfStatement))
153 return TokError("unexpected token in directive");
155 getStreamer().EmitELFSize(Sym, Expr);
159 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
160 // A section name can contain -, so we cannot just use
162 SMLoc FirstLoc = getLexer().getLoc();
169 SMLoc PrevLoc = getLexer().getLoc();
170 if (getLexer().is(AsmToken::Minus)) {
172 Lex(); // Consume the "-".
173 } else if (!getParser().ParseIdentifier(Tmp))
174 CurSize = Tmp.size();
179 SectionName = StringRef(FirstLoc.getPointer(), Size);
181 // Make sure the following token is adjacent.
182 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
191 // FIXME: This is a work in progress.
192 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
193 StringRef SectionName;
195 if (ParseSectionName(SectionName))
196 return TokError("expected identifier in directive");
198 std::string FlagsStr;
201 if (getLexer().is(AsmToken::Comma)) {
204 if (getLexer().isNot(AsmToken::String))
205 return TokError("expected string in directive");
207 FlagsStr = getTok().getStringContents();
210 AsmToken::TokenKind TypeStartToken;
211 if (getContext().getAsmInfo().getCommentString()[0] == '@')
212 TypeStartToken = AsmToken::Percent;
214 TypeStartToken = AsmToken::At;
216 if (getLexer().is(AsmToken::Comma)) {
218 if (getLexer().is(TypeStartToken)) {
220 if (getParser().ParseIdentifier(TypeName))
221 return TokError("expected identifier in directive");
223 if (getLexer().is(AsmToken::Comma)) {
226 if (getParser().ParseAbsoluteExpression(Size))
230 return TokError("section size must be positive");
236 if (getLexer().isNot(AsmToken::EndOfStatement))
237 return TokError("unexpected token in directive");
240 for (unsigned i = 0; i < FlagsStr.size(); i++) {
241 switch (FlagsStr[i]) {
243 Flags |= MCSectionELF::SHF_ALLOC;
246 Flags |= MCSectionELF::SHF_EXECINSTR;
249 Flags |= MCSectionELF::SHF_WRITE;
252 Flags |= MCSectionELF::SHF_MERGE;
255 Flags |= MCSectionELF::SHF_STRINGS;
258 Flags |= MCSectionELF::SHF_TLS;
261 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
264 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
267 return TokError("unknown flag");
271 unsigned Type = MCSectionELF::SHT_NULL;
272 if (!TypeName.empty()) {
273 if (TypeName == "init_array")
274 Type = MCSectionELF::SHT_INIT_ARRAY;
275 else if (TypeName == "fini_array")
276 Type = MCSectionELF::SHT_FINI_ARRAY;
277 else if (TypeName == "preinit_array")
278 Type = MCSectionELF::SHT_PREINIT_ARRAY;
279 else if (TypeName == "nobits")
280 Type = MCSectionELF::SHT_NOBITS;
281 else if (TypeName == "progbits")
282 Type = MCSectionELF::SHT_PROGBITS;
284 return TokError("unknown section type");
287 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
288 ? SectionKind::getText()
289 : SectionKind::getDataRel();
290 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
296 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
297 const MCSection *PreviousSection = getStreamer().getPreviousSection();
298 if (PreviousSection != NULL)
299 getStreamer().SwitchSection(PreviousSection);
304 /// ParseDirectiveELFType
305 /// ::= .type identifier , @attribute
306 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
308 if (getParser().ParseIdentifier(Name))
309 return TokError("expected identifier in directive");
311 // Handle the identifier as the key symbol.
312 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
314 if (getLexer().isNot(AsmToken::Comma))
315 return TokError("unexpected token in '.type' directive");
318 if (getLexer().isNot(AsmToken::At))
319 return TokError("expected '@' before type");
325 TypeLoc = getLexer().getLoc();
326 if (getParser().ParseIdentifier(Type))
327 return TokError("expected symbol type in directive");
329 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
330 .Case("function", MCSA_ELF_TypeFunction)
331 .Case("object", MCSA_ELF_TypeObject)
332 .Case("tls_object", MCSA_ELF_TypeTLS)
333 .Case("common", MCSA_ELF_TypeCommon)
334 .Case("notype", MCSA_ELF_TypeNoType)
335 .Default(MCSA_Invalid);
337 if (Attr == MCSA_Invalid)
338 return Error(TypeLoc, "unsupported attribute in '.type' directive");
340 if (getLexer().isNot(AsmToken::EndOfStatement))
341 return TokError("unexpected token in '.type' directive");
345 getStreamer().EmitSymbolAttribute(Sym, Attr);
350 /// ParseDirectiveIdent
351 /// ::= .ident string
352 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
353 if (getLexer().isNot(AsmToken::String))
354 return TokError("unexpected token in '.ident' directive");
356 StringRef Data = getTok().getIdentifier();
360 const MCSection *OldSection = getStreamer().getCurrentSection();
361 const MCSection *Comment =
362 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
363 MCSectionELF::SHF_MERGE |
364 MCSectionELF::SHF_STRINGS,
365 SectionKind::getReadOnly(),
368 static bool First = true;
370 getStreamer().SwitchSection(Comment);
372 getStreamer().EmitIntValue(0, 1);
374 getStreamer().EmitBytes(Data, 0);
375 getStreamer().EmitIntValue(0, 1);
376 getStreamer().SwitchSection(OldSection);
382 MCAsmParserExtension *createELFAsmParser() {
383 return new ELFAsmParser;