blob: 7b88ea2d1da39949d39bb511562c6d16562fcdbd [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Michael J. Spencere90ea132010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Rafael Espindola88182132010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000020using namespace llvm;
21
22namespace {
23
24class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000025 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
26 void AddDirectiveHandler(StringRef Directive) {
27 getParser().AddDirectiveHandler(this, Directive,
28 HandleDirective<ELFAsmParser, Handler>);
29 }
30
Daniel Dunbar5146a092010-07-12 21:23:32 +000031 bool ParseSectionSwitch(StringRef Section, unsigned Type,
32 unsigned Flags, SectionKind Kind);
33
34public:
35 ELFAsmParser() {}
36
37 virtual void Initialize(MCAsmParser &Parser) {
38 // Call the base implementation.
39 this->MCAsmParserExtension::Initialize(Parser);
40
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000041 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000043 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000051 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000053 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000054 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000055 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000056 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000057 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000058 }
59
Rafael Espindolad80781b2010-09-15 21:48:40 +000060 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
61 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000062 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000063 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000064 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000065 SectionKind::getDataRel());
66 }
67 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000068 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000069 ELF::SHF_EXECINSTR |
70 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000071 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000072 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000073 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000074 ELF::SHF_WRITE |
75 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000076 }
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000078 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000079 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000080 SectionKind::getReadOnly());
81 }
82 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000084 ELF::SHF_ALLOC |
85 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000086 SectionKind::getThreadData());
87 }
88 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000090 ELF::SHF_ALLOC |
91 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000092 SectionKind::getThreadBSS());
93 }
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000095 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000096 ELF::SHF_ALLOC |
97 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000098 SectionKind::getDataRel());
99 }
100 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000101 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000102 ELF::SHF_ALLOC |
103 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000104 SectionKind::getReadOnlyWithRel());
105 }
106 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000107 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000108 ELF::SHF_ALLOC |
109 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000110 SectionKind::getReadOnlyWithRelLocal());
111 }
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000113 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000114 ELF::SHF_ALLOC |
115 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000116 SectionKind::getDataRel());
117 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000120 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000121 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000122 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000123 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000124 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000125
126private:
127 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000128};
129
130}
131
132bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
133 unsigned Flags, SectionKind Kind) {
134 if (getLexer().isNot(AsmToken::EndOfStatement))
135 return TokError("unexpected token in section switching directive");
136 Lex();
137
138 getStreamer().SwitchSection(getContext().getELFSection(
139 Section, Type, Flags, Kind));
140
141 return false;
142}
143
Eli Friedman21444ef2010-07-17 04:29:04 +0000144bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000145 StringRef Name;
146 if (getParser().ParseIdentifier(Name))
147 return TokError("expected identifier in directive");
148 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
149
150 if (getLexer().isNot(AsmToken::Comma))
151 return TokError("unexpected token in directive");
152 Lex();
153
154 const MCExpr *Expr;
155 if (getParser().ParseExpression(Expr))
156 return true;
157
158 if (getLexer().isNot(AsmToken::EndOfStatement))
159 return TokError("unexpected token in directive");
160
161 getStreamer().EmitELFSize(Sym, Expr);
162 return false;
163}
164
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000165bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
166 // A section name can contain -, so we cannot just use
167 // ParseIdentifier.
168 SMLoc FirstLoc = getLexer().getLoc();
169 unsigned Size = 0;
170
Rafael Espindola184640e2011-01-24 18:02:54 +0000171 if (getLexer().is(AsmToken::String)) {
172 SectionName = getTok().getIdentifier();
173 Lex();
174 return false;
175 }
176
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000177 for (;;) {
178 StringRef Tmp;
179 unsigned CurSize;
180
181 SMLoc PrevLoc = getLexer().getLoc();
182 if (getLexer().is(AsmToken::Minus)) {
183 CurSize = 1;
184 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000185 } else if (getLexer().is(AsmToken::String)) {
186 CurSize = getTok().getIdentifier().size() + 2;
187 Lex();
188 } else if (getLexer().is(AsmToken::Identifier)) {
189 CurSize = getTok().getIdentifier().size();
190 Lex();
191 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000192 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000193 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000194
195 Size += CurSize;
196 SectionName = StringRef(FirstLoc.getPointer(), Size);
197
198 // Make sure the following token is adjacent.
199 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
200 break;
201 }
202 if (Size == 0)
203 return true;
204
205 return false;
206}
207
Rafael Espindola25958732010-11-24 21:57:39 +0000208static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000209 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000210 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000211 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000212 return SectionKind::getThreadData();
213 return SectionKind::getDataRel();
214}
215
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000216static int parseSectionFlags(StringRef flagsStr) {
217 int flags = 0;
218
219 for (unsigned i = 0; i < flagsStr.size(); i++) {
220 switch (flagsStr[i]) {
221 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000222 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000223 break;
224 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000225 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000226 break;
227 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000228 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000229 break;
230 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000231 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000232 break;
233 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000234 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000235 break;
236 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000237 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000238 break;
239 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000240 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000241 break;
242 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000243 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000244 break;
245 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000246 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000247 break;
248 default:
249 return -1;
250 }
251 }
252
253 return flags;
254}
255
Eli Friedman21444ef2010-07-17 04:29:04 +0000256// FIXME: This is a work in progress.
257bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
258 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000259
260 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000261 return TokError("expected identifier in directive");
262
Eli Friedman21444ef2010-07-17 04:29:04 +0000263 StringRef TypeName;
264 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000265 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000266 unsigned Flags = 0;
267
268 // Set the defaults first.
269 if (SectionName == ".fini" || SectionName == ".init" ||
270 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000271 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000272 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000273 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000274
Eli Friedman21444ef2010-07-17 04:29:04 +0000275 if (getLexer().is(AsmToken::Comma)) {
276 Lex();
277
278 if (getLexer().isNot(AsmToken::String))
279 return TokError("expected string in directive");
280
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000281 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000282 Lex();
283
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000284 int extraFlags = parseSectionFlags(FlagsStr);
285 if (extraFlags < 0)
286 return TokError("unknown flag");
287 Flags |= extraFlags;
288
Rafael Espindola1c130262011-01-23 04:43:11 +0000289 bool Mergeable = Flags & ELF::SHF_MERGE;
290 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000291
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000292 if (getLexer().isNot(AsmToken::Comma)) {
293 if (Mergeable)
294 return TokError("Mergeable section must specify the type");
295 if (Group)
296 return TokError("Group section must specify the type");
297 } else {
298 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000299 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
300 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000301
302 Lex();
303 if (getParser().ParseIdentifier(TypeName))
304 return TokError("expected identifier in directive");
305
306 if (Mergeable) {
307 if (getLexer().isNot(AsmToken::Comma))
308 return TokError("expected the entry size");
309 Lex();
310 if (getParser().ParseAbsoluteExpression(Size))
311 return true;
312 if (Size <= 0)
313 return TokError("entry size must be positive");
314 }
315
316 if (Group) {
317 if (getLexer().isNot(AsmToken::Comma))
318 return TokError("expected group name");
319 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000320 if (getParser().ParseIdentifier(GroupName))
321 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000322 if (getLexer().is(AsmToken::Comma)) {
323 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000324 StringRef Linkage;
325 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000326 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000327 if (Linkage != "comdat")
328 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000329 }
330 }
331 }
332 }
333
334 if (getLexer().isNot(AsmToken::EndOfStatement))
335 return TokError("unexpected token in directive");
336
Rafael Espindolac85dca62011-01-23 04:28:49 +0000337 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000338
Eli Friedman21444ef2010-07-17 04:29:04 +0000339 if (!TypeName.empty()) {
340 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000341 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000342 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000343 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000344 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000345 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000346 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000347 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000348 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000349 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000350 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000351 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000352 else if (TypeName == "unwind")
353 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000354 else
355 return TokError("unknown section type");
356 }
357
Rafael Espindola25958732010-11-24 21:57:39 +0000358 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000359 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000360 Flags, Kind, Size,
361 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000362 return false;
363}
364
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000365bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
366 const MCSection *PreviousSection = getStreamer().getPreviousSection();
367 if (PreviousSection != NULL)
368 getStreamer().SwitchSection(PreviousSection);
369
370 return false;
371}
372
Michael J. Spencere90ea132010-10-09 03:47:55 +0000373/// ParseDirectiveELFType
374/// ::= .type identifier , @attribute
375bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
376 StringRef Name;
377 if (getParser().ParseIdentifier(Name))
378 return TokError("expected identifier in directive");
379
380 // Handle the identifier as the key symbol.
381 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
382
383 if (getLexer().isNot(AsmToken::Comma))
384 return TokError("unexpected token in '.type' directive");
385 Lex();
386
Rafael Espindolad4a35262010-11-12 15:47:08 +0000387 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
388 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000389 Lex();
390
391 StringRef Type;
392 SMLoc TypeLoc;
393
394 TypeLoc = getLexer().getLoc();
395 if (getParser().ParseIdentifier(Type))
396 return TokError("expected symbol type in directive");
397
398 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
399 .Case("function", MCSA_ELF_TypeFunction)
400 .Case("object", MCSA_ELF_TypeObject)
401 .Case("tls_object", MCSA_ELF_TypeTLS)
402 .Case("common", MCSA_ELF_TypeCommon)
403 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000404 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000405 .Default(MCSA_Invalid);
406
407 if (Attr == MCSA_Invalid)
408 return Error(TypeLoc, "unsupported attribute in '.type' directive");
409
410 if (getLexer().isNot(AsmToken::EndOfStatement))
411 return TokError("unexpected token in '.type' directive");
412
413 Lex();
414
415 getStreamer().EmitSymbolAttribute(Sym, Attr);
416
417 return false;
418}
419
Rafael Espindola61e3b912010-10-26 19:35:47 +0000420/// ParseDirectiveIdent
421/// ::= .ident string
422bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
423 if (getLexer().isNot(AsmToken::String))
424 return TokError("unexpected token in '.ident' directive");
425
426 StringRef Data = getTok().getIdentifier();
427
428 Lex();
429
430 const MCSection *OldSection = getStreamer().getCurrentSection();
431 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000432 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000433 ELF::SHF_MERGE |
434 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000435 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000436 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000437
438 static bool First = true;
439
440 getStreamer().SwitchSection(Comment);
441 if (First)
442 getStreamer().EmitIntValue(0, 1);
443 First = false;
444 getStreamer().EmitBytes(Data, 0);
445 getStreamer().EmitIntValue(0, 1);
446 getStreamer().SwitchSection(OldSection);
447 return false;
448}
449
Rafael Espindola88182132010-10-27 15:18:17 +0000450/// ParseDirectiveSymver
451/// ::= .symver foo, bar2@zed
452bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
453 StringRef Name;
454 if (getParser().ParseIdentifier(Name))
455 return TokError("expected identifier in directive");
456
457 if (getLexer().isNot(AsmToken::Comma))
458 return TokError("expected a comma");
459
460 Lex();
461
462 StringRef AliasName;
463 if (getParser().ParseIdentifier(AliasName))
464 return TokError("expected identifier in directive");
465
466 if (AliasName.find('@') == StringRef::npos)
467 return TokError("expected a '@' in the name");
468
469 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
470 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
471 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
472
473 getStreamer().EmitAssignment(Alias, Value);
474 return false;
475}
476
Rafael Espindola484291c2010-11-01 14:28:48 +0000477/// ParseDirectiveWeakref
478/// ::= .weakref foo, bar
479bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
480 // FIXME: Share code with the other alias building directives.
481
482 StringRef AliasName;
483 if (getParser().ParseIdentifier(AliasName))
484 return TokError("expected identifier in directive");
485
486 if (getLexer().isNot(AsmToken::Comma))
487 return TokError("expected a comma");
488
489 Lex();
490
491 StringRef Name;
492 if (getParser().ParseIdentifier(Name))
493 return TokError("expected identifier in directive");
494
495 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
496
497 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
498
499 getStreamer().EmitWeakReference(Alias, Sym);
500 return false;
501}
502
Daniel Dunbar5146a092010-07-12 21:23:32 +0000503namespace llvm {
504
505MCAsmParserExtension *createELFAsmParser() {
506 return new ELFAsmParser;
507}
508
509}