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/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Support/ELF.h"
24 class ELFAsmParser
: public MCAsmParserExtension
{
25 template<bool (ELFAsmParser::*Handler
)(StringRef
, SMLoc
)>
26 void AddDirectiveHandler(StringRef Directive
) {
27 getParser().AddDirectiveHandler(this, Directive
,
28 HandleDirective
<ELFAsmParser
, Handler
>);
31 bool ParseSectionSwitch(StringRef Section
, unsigned Type
,
32 unsigned Flags
, SectionKind Kind
);
36 ELFAsmParser() : SeenIdent(false) {
37 BracketExpressionsSupported
= true;
40 virtual void Initialize(MCAsmParser
&Parser
) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser
);
44 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveData
>(".data");
45 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveText
>(".text");
46 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveBSS
>(".bss");
47 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveRoData
>(".rodata");
48 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveTData
>(".tdata");
49 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveTBSS
>(".tbss");
50 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveDataRel
>(".data.rel");
51 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveDataRelRo
>(".data.rel.ro");
52 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal
>(".data.rel.ro.local");
53 AddDirectiveHandler
<&ELFAsmParser::ParseSectionDirectiveEhFrame
>(".eh_frame");
54 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveSection
>(".section");
55 AddDirectiveHandler
<&ELFAsmParser::ParseDirectivePushSection
>(".pushsection");
56 AddDirectiveHandler
<&ELFAsmParser::ParseDirectivePopSection
>(".popsection");
57 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveSize
>(".size");
58 AddDirectiveHandler
<&ELFAsmParser::ParseDirectivePrevious
>(".previous");
59 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveType
>(".type");
60 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveIdent
>(".ident");
61 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveSymver
>(".symver");
62 AddDirectiveHandler
<&ELFAsmParser::ParseDirectiveWeakref
>(".weakref");
65 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
66 // the best way for us to get access to it?
67 bool ParseSectionDirectiveData(StringRef
, SMLoc
) {
68 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS
,
69 ELF::SHF_WRITE
|ELF::SHF_ALLOC
,
70 SectionKind::getDataRel());
72 bool ParseSectionDirectiveText(StringRef
, SMLoc
) {
73 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS
,
75 ELF::SHF_ALLOC
, SectionKind::getText());
77 bool ParseSectionDirectiveBSS(StringRef
, SMLoc
) {
78 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS
,
80 ELF::SHF_ALLOC
, SectionKind::getBSS());
82 bool ParseSectionDirectiveRoData(StringRef
, SMLoc
) {
83 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS
,
85 SectionKind::getReadOnly());
87 bool ParseSectionDirectiveTData(StringRef
, SMLoc
) {
88 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS
,
90 ELF::SHF_TLS
| ELF::SHF_WRITE
,
91 SectionKind::getThreadData());
93 bool ParseSectionDirectiveTBSS(StringRef
, SMLoc
) {
94 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS
,
96 ELF::SHF_TLS
| ELF::SHF_WRITE
,
97 SectionKind::getThreadBSS());
99 bool ParseSectionDirectiveDataRel(StringRef
, SMLoc
) {
100 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS
,
103 SectionKind::getDataRel());
105 bool ParseSectionDirectiveDataRelRo(StringRef
, SMLoc
) {
106 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS
,
109 SectionKind::getReadOnlyWithRel());
111 bool ParseSectionDirectiveDataRelRoLocal(StringRef
, SMLoc
) {
112 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS
,
115 SectionKind::getReadOnlyWithRelLocal());
117 bool ParseSectionDirectiveEhFrame(StringRef
, SMLoc
) {
118 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS
,
121 SectionKind::getDataRel());
123 bool ParseDirectivePushSection(StringRef
, SMLoc
);
124 bool ParseDirectivePopSection(StringRef
, SMLoc
);
125 bool ParseDirectiveSection(StringRef
, SMLoc
);
126 bool ParseDirectiveSize(StringRef
, SMLoc
);
127 bool ParseDirectivePrevious(StringRef
, SMLoc
);
128 bool ParseDirectiveType(StringRef
, SMLoc
);
129 bool ParseDirectiveIdent(StringRef
, SMLoc
);
130 bool ParseDirectiveSymver(StringRef
, SMLoc
);
131 bool ParseDirectiveWeakref(StringRef
, SMLoc
);
134 bool ParseSectionName(StringRef
&SectionName
);
139 bool ELFAsmParser::ParseSectionSwitch(StringRef Section
, unsigned Type
,
140 unsigned Flags
, SectionKind Kind
) {
141 if (getLexer().isNot(AsmToken::EndOfStatement
))
142 return TokError("unexpected token in section switching directive");
145 getStreamer().SwitchSection(getContext().getELFSection(
146 Section
, Type
, Flags
, Kind
));
151 bool ELFAsmParser::ParseDirectiveSize(StringRef
, SMLoc
) {
153 if (getParser().ParseIdentifier(Name
))
154 return TokError("expected identifier in directive");
155 MCSymbol
*Sym
= getContext().GetOrCreateSymbol(Name
);;
157 if (getLexer().isNot(AsmToken::Comma
))
158 return TokError("unexpected token in directive");
162 if (getParser().ParseExpression(Expr
))
165 if (getLexer().isNot(AsmToken::EndOfStatement
))
166 return TokError("unexpected token in directive");
168 getStreamer().EmitELFSize(Sym
, Expr
);
172 bool ELFAsmParser::ParseSectionName(StringRef
&SectionName
) {
173 // A section name can contain -, so we cannot just use
175 SMLoc FirstLoc
= getLexer().getLoc();
178 if (getLexer().is(AsmToken::String
)) {
179 SectionName
= getTok().getIdentifier();
188 SMLoc PrevLoc
= getLexer().getLoc();
189 if (getLexer().is(AsmToken::Minus
)) {
191 Lex(); // Consume the "-".
192 } else if (getLexer().is(AsmToken::String
)) {
193 CurSize
= getTok().getIdentifier().size() + 2;
195 } else if (getLexer().is(AsmToken::Identifier
)) {
196 CurSize
= getTok().getIdentifier().size();
203 SectionName
= StringRef(FirstLoc
.getPointer(), Size
);
205 // Make sure the following token is adjacent.
206 if (PrevLoc
.getPointer() + CurSize
!= getTok().getLoc().getPointer())
215 static SectionKind
computeSectionKind(unsigned Flags
) {
216 if (Flags
& ELF::SHF_EXECINSTR
)
217 return SectionKind::getText();
218 if (Flags
& ELF::SHF_TLS
)
219 return SectionKind::getThreadData();
220 return SectionKind::getDataRel();
223 static int parseSectionFlags(StringRef flagsStr
) {
226 for (unsigned i
= 0; i
< flagsStr
.size(); i
++) {
227 switch (flagsStr
[i
]) {
229 flags
|= ELF::SHF_ALLOC
;
232 flags
|= ELF::SHF_EXECINSTR
;
235 flags
|= ELF::SHF_WRITE
;
238 flags
|= ELF::SHF_MERGE
;
241 flags
|= ELF::SHF_STRINGS
;
244 flags
|= ELF::SHF_TLS
;
247 flags
|= ELF::XCORE_SHF_CP_SECTION
;
250 flags
|= ELF::XCORE_SHF_DP_SECTION
;
253 flags
|= ELF::SHF_GROUP
;
263 bool ELFAsmParser::ParseDirectivePushSection(StringRef s
, SMLoc loc
) {
264 getStreamer().PushSection();
266 if (ParseDirectiveSection(s
, loc
)) {
267 getStreamer().PopSection();
274 bool ELFAsmParser::ParseDirectivePopSection(StringRef
, SMLoc
) {
275 if (!getStreamer().PopSection())
276 return TokError(".popsection without corresponding .pushsection");
280 // FIXME: This is a work in progress.
281 bool ELFAsmParser::ParseDirectiveSection(StringRef
, SMLoc
) {
282 StringRef SectionName
;
284 if (ParseSectionName(SectionName
))
285 return TokError("expected identifier in directive");
292 // Set the defaults first.
293 if (SectionName
== ".fini" || SectionName
== ".init" ||
294 SectionName
== ".rodata")
295 Flags
|= ELF::SHF_ALLOC
;
296 if (SectionName
== ".fini" || SectionName
== ".init")
297 Flags
|= ELF::SHF_EXECINSTR
;
299 if (getLexer().is(AsmToken::Comma
)) {
302 if (getLexer().isNot(AsmToken::String
))
303 return TokError("expected string in directive");
305 StringRef FlagsStr
= getTok().getStringContents();
308 int extraFlags
= parseSectionFlags(FlagsStr
);
310 return TokError("unknown flag");
313 bool Mergeable
= Flags
& ELF::SHF_MERGE
;
314 bool Group
= Flags
& ELF::SHF_GROUP
;
316 if (getLexer().isNot(AsmToken::Comma
)) {
318 return TokError("Mergeable section must specify the type");
320 return TokError("Group section must specify the type");
323 if (getLexer().isNot(AsmToken::Percent
) && getLexer().isNot(AsmToken::At
))
324 return TokError("expected '@' or '%' before type");
327 if (getParser().ParseIdentifier(TypeName
))
328 return TokError("expected identifier in directive");
331 if (getLexer().isNot(AsmToken::Comma
))
332 return TokError("expected the entry size");
334 if (getParser().ParseAbsoluteExpression(Size
))
337 return TokError("entry size must be positive");
341 if (getLexer().isNot(AsmToken::Comma
))
342 return TokError("expected group name");
344 if (getParser().ParseIdentifier(GroupName
))
346 if (getLexer().is(AsmToken::Comma
)) {
349 if (getParser().ParseIdentifier(Linkage
))
351 if (Linkage
!= "comdat")
352 return TokError("Linkage must be 'comdat'");
358 if (getLexer().isNot(AsmToken::EndOfStatement
))
359 return TokError("unexpected token in directive");
361 unsigned Type
= ELF::SHT_PROGBITS
;
363 if (!TypeName
.empty()) {
364 if (TypeName
== "init_array")
365 Type
= ELF::SHT_INIT_ARRAY
;
366 else if (TypeName
== "fini_array")
367 Type
= ELF::SHT_FINI_ARRAY
;
368 else if (TypeName
== "preinit_array")
369 Type
= ELF::SHT_PREINIT_ARRAY
;
370 else if (TypeName
== "nobits")
371 Type
= ELF::SHT_NOBITS
;
372 else if (TypeName
== "progbits")
373 Type
= ELF::SHT_PROGBITS
;
374 else if (TypeName
== "note")
375 Type
= ELF::SHT_NOTE
;
376 else if (TypeName
== "unwind")
377 Type
= ELF::SHT_X86_64_UNWIND
;
379 return TokError("unknown section type");
382 SectionKind Kind
= computeSectionKind(Flags
);
383 getStreamer().SwitchSection(getContext().getELFSection(SectionName
, Type
,
389 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName
, SMLoc
) {
390 const MCSection
*PreviousSection
= getStreamer().getPreviousSection();
391 if (PreviousSection
== NULL
)
392 return TokError(".previous without corresponding .section");
393 getStreamer().SwitchSection(PreviousSection
);
398 /// ParseDirectiveELFType
399 /// ::= .type identifier , @attribute
400 bool ELFAsmParser::ParseDirectiveType(StringRef
, SMLoc
) {
402 if (getParser().ParseIdentifier(Name
))
403 return TokError("expected identifier in directive");
405 // Handle the identifier as the key symbol.
406 MCSymbol
*Sym
= getContext().GetOrCreateSymbol(Name
);
408 if (getLexer().isNot(AsmToken::Comma
))
409 return TokError("unexpected token in '.type' directive");
412 if (getLexer().isNot(AsmToken::Percent
) && getLexer().isNot(AsmToken::At
))
413 return TokError("expected '@' or '%' before type");
419 TypeLoc
= getLexer().getLoc();
420 if (getParser().ParseIdentifier(Type
))
421 return TokError("expected symbol type in directive");
423 MCSymbolAttr Attr
= StringSwitch
<MCSymbolAttr
>(Type
)
424 .Case("function", MCSA_ELF_TypeFunction
)
425 .Case("object", MCSA_ELF_TypeObject
)
426 .Case("tls_object", MCSA_ELF_TypeTLS
)
427 .Case("common", MCSA_ELF_TypeCommon
)
428 .Case("notype", MCSA_ELF_TypeNoType
)
429 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject
)
430 .Default(MCSA_Invalid
);
432 if (Attr
== MCSA_Invalid
)
433 return Error(TypeLoc
, "unsupported attribute in '.type' directive");
435 if (getLexer().isNot(AsmToken::EndOfStatement
))
436 return TokError("unexpected token in '.type' directive");
440 getStreamer().EmitSymbolAttribute(Sym
, Attr
);
445 /// ParseDirectiveIdent
446 /// ::= .ident string
447 bool ELFAsmParser::ParseDirectiveIdent(StringRef
, SMLoc
) {
448 if (getLexer().isNot(AsmToken::String
))
449 return TokError("unexpected token in '.ident' directive");
451 StringRef Data
= getTok().getIdentifier();
455 const MCSection
*Comment
=
456 getContext().getELFSection(".comment", ELF::SHT_PROGBITS
,
459 SectionKind::getReadOnly(),
462 getStreamer().PushSection();
463 getStreamer().SwitchSection(Comment
);
465 getStreamer().EmitIntValue(0, 1);
468 getStreamer().EmitBytes(Data
, 0);
469 getStreamer().EmitIntValue(0, 1);
470 getStreamer().PopSection();
474 /// ParseDirectiveSymver
475 /// ::= .symver foo, bar2@zed
476 bool ELFAsmParser::ParseDirectiveSymver(StringRef
, SMLoc
) {
478 if (getParser().ParseIdentifier(Name
))
479 return TokError("expected identifier in directive");
481 if (getLexer().isNot(AsmToken::Comma
))
482 return TokError("expected a comma");
487 if (getParser().ParseIdentifier(AliasName
))
488 return TokError("expected identifier in directive");
490 if (AliasName
.find('@') == StringRef::npos
)
491 return TokError("expected a '@' in the name");
493 MCSymbol
*Alias
= getContext().GetOrCreateSymbol(AliasName
);
494 MCSymbol
*Sym
= getContext().GetOrCreateSymbol(Name
);
495 const MCExpr
*Value
= MCSymbolRefExpr::Create(Sym
, getContext());
497 getStreamer().EmitAssignment(Alias
, Value
);
501 /// ParseDirectiveWeakref
502 /// ::= .weakref foo, bar
503 bool ELFAsmParser::ParseDirectiveWeakref(StringRef
, SMLoc
) {
504 // FIXME: Share code with the other alias building directives.
507 if (getParser().ParseIdentifier(AliasName
))
508 return TokError("expected identifier in directive");
510 if (getLexer().isNot(AsmToken::Comma
))
511 return TokError("expected a comma");
516 if (getParser().ParseIdentifier(Name
))
517 return TokError("expected identifier in directive");
519 MCSymbol
*Alias
= getContext().GetOrCreateSymbol(AliasName
);
521 MCSymbol
*Sym
= getContext().GetOrCreateSymbol(Name
);
523 getStreamer().EmitWeakReference(Alias
, Sym
);
529 MCAsmParserExtension
*createELFAsmParser() {
530 return new ELFAsmParser
;