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/Twine.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/ADT/Twine.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");
54 bool ParseSectionDirectiveData(StringRef, SMLoc) {
55 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
56 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
57 SectionKind::getDataRel());
59 bool ParseSectionDirectiveText(StringRef, SMLoc) {
60 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
61 MCSectionELF::SHF_EXECINSTR |
62 MCSectionELF::SHF_ALLOC, SectionKind::getText());
64 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
65 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
66 MCSectionELF::SHF_WRITE |
67 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
69 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
70 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
71 MCSectionELF::SHF_ALLOC,
72 SectionKind::getReadOnly());
74 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
75 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
76 MCSectionELF::SHF_ALLOC |
77 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
78 SectionKind::getThreadData());
80 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
81 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
82 MCSectionELF::SHF_ALLOC |
83 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
84 SectionKind::getThreadBSS());
86 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
87 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
88 MCSectionELF::SHF_ALLOC |
89 MCSectionELF::SHF_WRITE,
90 SectionKind::getDataRel());
92 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
93 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
94 MCSectionELF::SHF_ALLOC |
95 MCSectionELF::SHF_WRITE,
96 SectionKind::getReadOnlyWithRel());
98 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
99 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
100 MCSectionELF::SHF_ALLOC |
101 MCSectionELF::SHF_WRITE,
102 SectionKind::getReadOnlyWithRelLocal());
104 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
105 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
106 MCSectionELF::SHF_ALLOC |
107 MCSectionELF::SHF_WRITE,
108 SectionKind::getDataRel());
110 bool ParseDirectiveSection(StringRef, SMLoc);
111 bool ParseDirectiveSize(StringRef, SMLoc);
112 bool ParseDirectivePrevious(StringRef, SMLoc);
117 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
118 unsigned Flags, SectionKind Kind) {
119 if (getLexer().isNot(AsmToken::EndOfStatement))
120 return TokError("unexpected token in section switching directive");
123 getStreamer().SwitchSection(getContext().getELFSection(
124 Section, Type, Flags, Kind));
129 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
131 if (getParser().ParseIdentifier(Name))
132 return TokError("expected identifier in directive");
133 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
135 if (getLexer().isNot(AsmToken::Comma))
136 return TokError("unexpected token in directive");
140 if (getParser().ParseExpression(Expr))
143 if (getLexer().isNot(AsmToken::EndOfStatement))
144 return TokError("unexpected token in directive");
146 getStreamer().EmitELFSize(Sym, Expr);
150 // FIXME: This is a work in progress.
151 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
152 StringRef SectionName;
153 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
154 if (getParser().ParseIdentifier(SectionName))
155 return TokError("expected identifier in directive");
157 std::string FlagsStr;
160 if (getLexer().is(AsmToken::Comma)) {
163 if (getLexer().isNot(AsmToken::String))
164 return TokError("expected string in directive");
166 FlagsStr = getTok().getStringContents();
169 AsmToken::TokenKind TypeStartToken;
170 if (getContext().getAsmInfo().getCommentString()[0] == '@')
171 TypeStartToken = AsmToken::Percent;
173 TypeStartToken = AsmToken::At;
175 if (getLexer().is(AsmToken::Comma)) {
177 if (getLexer().is(TypeStartToken)) {
179 if (getParser().ParseIdentifier(TypeName))
180 return TokError("expected identifier in directive");
182 if (getLexer().is(AsmToken::Comma)) {
185 if (getParser().ParseAbsoluteExpression(Size))
189 return TokError("section size must be positive");
195 if (getLexer().isNot(AsmToken::EndOfStatement))
196 return TokError("unexpected token in directive");
199 for (unsigned i = 0; i < FlagsStr.size(); i++) {
200 switch (FlagsStr[i]) {
202 Flags |= MCSectionELF::SHF_ALLOC;
205 Flags |= MCSectionELF::SHF_EXECINSTR;
208 Flags |= MCSectionELF::SHF_WRITE;
211 Flags |= MCSectionELF::SHF_MERGE;
214 Flags |= MCSectionELF::SHF_STRINGS;
217 Flags |= MCSectionELF::SHF_TLS;
220 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
223 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
226 return TokError("unknown flag");
230 unsigned Type = MCSectionELF::SHT_NULL;
231 if (!TypeName.empty()) {
232 if (TypeName == "init_array")
233 Type = MCSectionELF::SHT_INIT_ARRAY;
234 else if (TypeName == "fini_array")
235 Type = MCSectionELF::SHT_FINI_ARRAY;
236 else if (TypeName == "preinit_array")
237 Type = MCSectionELF::SHT_PREINIT_ARRAY;
238 else if (TypeName == "nobits")
239 Type = MCSectionELF::SHT_NOBITS;
240 else if (TypeName == "progbits")
241 Type = MCSectionELF::SHT_PROGBITS;
243 return TokError("unknown section type");
246 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
247 ? SectionKind::getText()
248 : SectionKind::getDataRel();
249 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
250 Flags, Kind, false));
254 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
255 const MCSection *PreviousSection = getStreamer().getPreviousSection();
256 if (PreviousSection != NULL)
257 getStreamer().SwitchSection(PreviousSection);
264 MCAsmParserExtension *createELFAsmParser() {
265 return new ELFAsmParser;