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 bool ret = ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
60 getStreamer().EmitCodeAlignment(4, 0);
63 bool ParseSectionDirectiveText(StringRef, SMLoc) {
64 bool ret = ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_EXECINSTR |
66 MCSectionELF::SHF_ALLOC, SectionKind::getText());
67 getStreamer().EmitCodeAlignment(4, 0);
70 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
71 bool ret = ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
72 MCSectionELF::SHF_WRITE |
73 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
74 getStreamer().EmitCodeAlignment(4, 0);
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
78 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
79 MCSectionELF::SHF_ALLOC,
80 SectionKind::getReadOnly());
82 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
84 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadData());
88 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
89 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
92 SectionKind::getThreadBSS());
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getDataRel());
100 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRel());
106 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
107 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getReadOnlyWithRelLocal());
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
113 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
114 MCSectionELF::SHF_ALLOC |
115 MCSectionELF::SHF_WRITE,
116 SectionKind::getDataRel());
118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
120 bool ParseDirectivePrevious(StringRef, SMLoc);
125 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
126 unsigned Flags, SectionKind Kind) {
127 if (getLexer().isNot(AsmToken::EndOfStatement))
128 return TokError("unexpected token in section switching directive");
131 getStreamer().SwitchSection(getContext().getELFSection(
132 Section, Type, Flags, Kind));
137 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
139 if (getParser().ParseIdentifier(Name))
140 return TokError("expected identifier in directive");
141 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
143 if (getLexer().isNot(AsmToken::Comma))
144 return TokError("unexpected token in directive");
148 if (getParser().ParseExpression(Expr))
151 if (getLexer().isNot(AsmToken::EndOfStatement))
152 return TokError("unexpected token in directive");
154 getStreamer().EmitELFSize(Sym, Expr);
158 // FIXME: This is a work in progress.
159 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
160 StringRef SectionName;
161 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
162 if (getParser().ParseIdentifier(SectionName))
163 return TokError("expected identifier in directive");
165 std::string FlagsStr;
168 if (getLexer().is(AsmToken::Comma)) {
171 if (getLexer().isNot(AsmToken::String))
172 return TokError("expected string in directive");
174 FlagsStr = getTok().getStringContents();
177 AsmToken::TokenKind TypeStartToken;
178 if (getContext().getAsmInfo().getCommentString()[0] == '@')
179 TypeStartToken = AsmToken::Percent;
181 TypeStartToken = AsmToken::At;
183 if (getLexer().is(AsmToken::Comma)) {
185 if (getLexer().is(TypeStartToken)) {
187 if (getParser().ParseIdentifier(TypeName))
188 return TokError("expected identifier in directive");
190 if (getLexer().is(AsmToken::Comma)) {
193 if (getParser().ParseAbsoluteExpression(Size))
197 return TokError("section size must be positive");
203 if (getLexer().isNot(AsmToken::EndOfStatement))
204 return TokError("unexpected token in directive");
207 for (unsigned i = 0; i < FlagsStr.size(); i++) {
208 switch (FlagsStr[i]) {
210 Flags |= MCSectionELF::SHF_ALLOC;
213 Flags |= MCSectionELF::SHF_EXECINSTR;
216 Flags |= MCSectionELF::SHF_WRITE;
219 Flags |= MCSectionELF::SHF_MERGE;
222 Flags |= MCSectionELF::SHF_STRINGS;
225 Flags |= MCSectionELF::SHF_TLS;
228 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
231 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
234 return TokError("unknown flag");
238 unsigned Type = MCSectionELF::SHT_NULL;
239 if (!TypeName.empty()) {
240 if (TypeName == "init_array")
241 Type = MCSectionELF::SHT_INIT_ARRAY;
242 else if (TypeName == "fini_array")
243 Type = MCSectionELF::SHT_FINI_ARRAY;
244 else if (TypeName == "preinit_array")
245 Type = MCSectionELF::SHT_PREINIT_ARRAY;
246 else if (TypeName == "nobits")
247 Type = MCSectionELF::SHT_NOBITS;
248 else if (TypeName == "progbits")
249 Type = MCSectionELF::SHT_PROGBITS;
251 return TokError("unknown section type");
254 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
255 ? SectionKind::getText()
256 : SectionKind::getDataRel();
257 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
258 Flags, Kind, false));
262 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
263 const MCSection *PreviousSection = getStreamer().getPreviousSection();
264 if (PreviousSection != NULL)
265 getStreamer().SwitchSection(PreviousSection);
272 MCAsmParserExtension *createELFAsmParser() {
273 return new ELFAsmParser;