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 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
55 // the best way for us to get access to it?
56 bool ParseSectionDirectiveData(StringRef, SMLoc) {
57 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
61 bool ParseSectionDirectiveText(StringRef, SMLoc) {
62 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
63 MCSectionELF::SHF_EXECINSTR |
64 MCSectionELF::SHF_ALLOC, SectionKind::getText());
66 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
67 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
68 MCSectionELF::SHF_WRITE |
69 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
71 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
72 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
73 MCSectionELF::SHF_ALLOC,
74 SectionKind::getReadOnly());
76 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
77 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_ALLOC |
79 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
80 SectionKind::getThreadData());
82 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
84 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadBSS());
88 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
89 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_WRITE,
92 SectionKind::getDataRel());
94 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getReadOnlyWithRel());
100 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRelLocal());
106 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
107 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getDataRel());
112 bool ParseDirectiveSection(StringRef, SMLoc);
113 bool ParseDirectiveSize(StringRef, SMLoc);
114 bool ParseDirectivePrevious(StringRef, SMLoc);
117 bool ParseSectionName(StringRef &SectionName);
122 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
123 unsigned Flags, SectionKind Kind) {
124 if (getLexer().isNot(AsmToken::EndOfStatement))
125 return TokError("unexpected token in section switching directive");
128 getStreamer().SwitchSection(getContext().getELFSection(
129 Section, Type, Flags, Kind));
134 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
136 if (getParser().ParseIdentifier(Name))
137 return TokError("expected identifier in directive");
138 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
140 if (getLexer().isNot(AsmToken::Comma))
141 return TokError("unexpected token in directive");
145 if (getParser().ParseExpression(Expr))
148 if (getLexer().isNot(AsmToken::EndOfStatement))
149 return TokError("unexpected token in directive");
151 getStreamer().EmitELFSize(Sym, Expr);
155 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
156 // A section name can contain -, so we cannot just use
158 SMLoc FirstLoc = getLexer().getLoc();
165 SMLoc PrevLoc = getLexer().getLoc();
166 if (getLexer().is(AsmToken::Minus)) {
168 Lex(); // Consume the "-".
169 } else if (!getParser().ParseIdentifier(Tmp))
170 CurSize = Tmp.size();
175 SectionName = StringRef(FirstLoc.getPointer(), Size);
177 // Make sure the following token is adjacent.
178 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
187 // FIXME: This is a work in progress.
188 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
189 StringRef SectionName;
191 if (ParseSectionName(SectionName))
192 return TokError("expected identifier in directive");
194 std::string FlagsStr;
197 if (getLexer().is(AsmToken::Comma)) {
200 if (getLexer().isNot(AsmToken::String))
201 return TokError("expected string in directive");
203 FlagsStr = getTok().getStringContents();
206 AsmToken::TokenKind TypeStartToken;
207 if (getContext().getAsmInfo().getCommentString()[0] == '@')
208 TypeStartToken = AsmToken::Percent;
210 TypeStartToken = AsmToken::At;
212 if (getLexer().is(AsmToken::Comma)) {
214 if (getLexer().is(TypeStartToken)) {
216 if (getParser().ParseIdentifier(TypeName))
217 return TokError("expected identifier in directive");
219 if (getLexer().is(AsmToken::Comma)) {
222 if (getParser().ParseAbsoluteExpression(Size))
226 return TokError("section size must be positive");
232 if (getLexer().isNot(AsmToken::EndOfStatement))
233 return TokError("unexpected token in directive");
236 for (unsigned i = 0; i < FlagsStr.size(); i++) {
237 switch (FlagsStr[i]) {
239 Flags |= MCSectionELF::SHF_ALLOC;
242 Flags |= MCSectionELF::SHF_EXECINSTR;
245 Flags |= MCSectionELF::SHF_WRITE;
248 Flags |= MCSectionELF::SHF_MERGE;
251 Flags |= MCSectionELF::SHF_STRINGS;
254 Flags |= MCSectionELF::SHF_TLS;
257 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
260 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
263 return TokError("unknown flag");
267 unsigned Type = MCSectionELF::SHT_NULL;
268 if (!TypeName.empty()) {
269 if (TypeName == "init_array")
270 Type = MCSectionELF::SHT_INIT_ARRAY;
271 else if (TypeName == "fini_array")
272 Type = MCSectionELF::SHT_FINI_ARRAY;
273 else if (TypeName == "preinit_array")
274 Type = MCSectionELF::SHT_PREINIT_ARRAY;
275 else if (TypeName == "nobits")
276 Type = MCSectionELF::SHT_NOBITS;
277 else if (TypeName == "progbits")
278 Type = MCSectionELF::SHT_PROGBITS;
280 return TokError("unknown section type");
283 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
284 ? SectionKind::getText()
285 : SectionKind::getDataRel();
286 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
287 Flags, Kind, false));
291 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
292 const MCSection *PreviousSection = getStreamer().getPreviousSection();
293 if (PreviousSection != NULL)
294 getStreamer().SwitchSection(PreviousSection);
301 MCAsmParserExtension *createELFAsmParser() {
302 return new ELFAsmParser;