blob: bfaf36a451b3d805c4bed62dc4d2723176ca476b [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");
Rafael Espindola7768a9d2011-02-16 01:08:29 +000052 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000054 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000055 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000056 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000057 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000058 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000059 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000060 }
61
Rafael Espindolad80781b2010-09-15 21:48:40 +000062 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
63 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000064 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000065 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000066 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000067 SectionKind::getDataRel());
68 }
69 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000070 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000071 ELF::SHF_EXECINSTR |
72 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000073 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000074 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000075 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000076 ELF::SHF_WRITE |
77 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000078 }
79 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000080 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000081 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000082 SectionKind::getReadOnly());
83 }
84 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000085 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000086 ELF::SHF_ALLOC |
87 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000088 SectionKind::getThreadData());
89 }
90 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000092 ELF::SHF_ALLOC |
93 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000094 SectionKind::getThreadBSS());
95 }
96 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000097 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000098 ELF::SHF_ALLOC |
99 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000100 SectionKind::getDataRel());
101 }
102 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000103 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000104 ELF::SHF_ALLOC |
105 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000106 SectionKind::getReadOnlyWithRel());
107 }
108 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000109 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000110 ELF::SHF_ALLOC |
111 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000112 SectionKind::getReadOnlyWithRelLocal());
113 }
114 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000115 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000116 ELF::SHF_ALLOC |
117 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000118 SectionKind::getDataRel());
119 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000120 bool ParseDirectivePushSection(StringRef, SMLoc);
121 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000122 bool ParseDirectiveSection(StringRef, SMLoc);
123 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000124 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000125 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000126 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000127 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000128 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000129
130private:
131 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000132};
133
134}
135
136bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
137 unsigned Flags, SectionKind Kind) {
138 if (getLexer().isNot(AsmToken::EndOfStatement))
139 return TokError("unexpected token in section switching directive");
140 Lex();
141
142 getStreamer().SwitchSection(getContext().getELFSection(
143 Section, Type, Flags, Kind));
144
145 return false;
146}
147
Eli Friedman21444ef2010-07-17 04:29:04 +0000148bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000149 StringRef Name;
150 if (getParser().ParseIdentifier(Name))
151 return TokError("expected identifier in directive");
152 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
153
154 if (getLexer().isNot(AsmToken::Comma))
155 return TokError("unexpected token in directive");
156 Lex();
157
158 const MCExpr *Expr;
159 if (getParser().ParseExpression(Expr))
160 return true;
161
162 if (getLexer().isNot(AsmToken::EndOfStatement))
163 return TokError("unexpected token in directive");
164
165 getStreamer().EmitELFSize(Sym, Expr);
166 return false;
167}
168
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000169bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
170 // A section name can contain -, so we cannot just use
171 // ParseIdentifier.
172 SMLoc FirstLoc = getLexer().getLoc();
173 unsigned Size = 0;
174
Rafael Espindola184640e2011-01-24 18:02:54 +0000175 if (getLexer().is(AsmToken::String)) {
176 SectionName = getTok().getIdentifier();
177 Lex();
178 return false;
179 }
180
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000181 for (;;) {
182 StringRef Tmp;
183 unsigned CurSize;
184
185 SMLoc PrevLoc = getLexer().getLoc();
186 if (getLexer().is(AsmToken::Minus)) {
187 CurSize = 1;
188 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000189 } else if (getLexer().is(AsmToken::String)) {
190 CurSize = getTok().getIdentifier().size() + 2;
191 Lex();
192 } else if (getLexer().is(AsmToken::Identifier)) {
193 CurSize = getTok().getIdentifier().size();
194 Lex();
195 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000196 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000197 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000198
199 Size += CurSize;
200 SectionName = StringRef(FirstLoc.getPointer(), Size);
201
202 // Make sure the following token is adjacent.
203 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
204 break;
205 }
206 if (Size == 0)
207 return true;
208
209 return false;
210}
211
Rafael Espindola25958732010-11-24 21:57:39 +0000212static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000213 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000214 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000215 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000216 return SectionKind::getThreadData();
217 return SectionKind::getDataRel();
218}
219
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000220static int parseSectionFlags(StringRef flagsStr) {
221 int flags = 0;
222
223 for (unsigned i = 0; i < flagsStr.size(); i++) {
224 switch (flagsStr[i]) {
225 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000226 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000227 break;
228 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000229 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000230 break;
231 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000232 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000233 break;
234 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000235 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000236 break;
237 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000238 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000239 break;
240 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000241 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000242 break;
243 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000244 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000245 break;
246 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000247 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000248 break;
249 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000250 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000251 break;
252 default:
253 return -1;
254 }
255 }
256
257 return flags;
258}
259
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000260bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
261 getStreamer().PushSection();
262
263 if (ParseDirectiveSection(s, loc)) {
264 getStreamer().PopSection();
265 return true;
266 }
267
268 return false;
269}
270
271bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
272 if (!getStreamer().PopSection())
273 return TokError(".popsection without corresponding .pushsection");
274 return false;
275}
276
Eli Friedman21444ef2010-07-17 04:29:04 +0000277// FIXME: This is a work in progress.
278bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
279 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000280
281 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000282 return TokError("expected identifier in directive");
283
Eli Friedman21444ef2010-07-17 04:29:04 +0000284 StringRef TypeName;
285 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000286 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000287 unsigned Flags = 0;
288
289 // Set the defaults first.
290 if (SectionName == ".fini" || SectionName == ".init" ||
291 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000292 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000293 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295
Eli Friedman21444ef2010-07-17 04:29:04 +0000296 if (getLexer().is(AsmToken::Comma)) {
297 Lex();
298
299 if (getLexer().isNot(AsmToken::String))
300 return TokError("expected string in directive");
301
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000302 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000303 Lex();
304
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000305 int extraFlags = parseSectionFlags(FlagsStr);
306 if (extraFlags < 0)
307 return TokError("unknown flag");
308 Flags |= extraFlags;
309
Rafael Espindola1c130262011-01-23 04:43:11 +0000310 bool Mergeable = Flags & ELF::SHF_MERGE;
311 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000312
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000313 if (getLexer().isNot(AsmToken::Comma)) {
314 if (Mergeable)
315 return TokError("Mergeable section must specify the type");
316 if (Group)
317 return TokError("Group section must specify the type");
318 } else {
319 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000320 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
321 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000322
323 Lex();
324 if (getParser().ParseIdentifier(TypeName))
325 return TokError("expected identifier in directive");
326
327 if (Mergeable) {
328 if (getLexer().isNot(AsmToken::Comma))
329 return TokError("expected the entry size");
330 Lex();
331 if (getParser().ParseAbsoluteExpression(Size))
332 return true;
333 if (Size <= 0)
334 return TokError("entry size must be positive");
335 }
336
337 if (Group) {
338 if (getLexer().isNot(AsmToken::Comma))
339 return TokError("expected group name");
340 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000341 if (getParser().ParseIdentifier(GroupName))
342 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000343 if (getLexer().is(AsmToken::Comma)) {
344 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000345 StringRef Linkage;
346 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000347 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000348 if (Linkage != "comdat")
349 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000350 }
351 }
352 }
353 }
354
355 if (getLexer().isNot(AsmToken::EndOfStatement))
356 return TokError("unexpected token in directive");
357
Rafael Espindolac85dca62011-01-23 04:28:49 +0000358 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000359
Eli Friedman21444ef2010-07-17 04:29:04 +0000360 if (!TypeName.empty()) {
361 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000362 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000363 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000364 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000365 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000366 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000367 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000368 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000369 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000370 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000371 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000372 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000373 else if (TypeName == "unwind")
374 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000375 else
376 return TokError("unknown section type");
377 }
378
Rafael Espindola25958732010-11-24 21:57:39 +0000379 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000380 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000381 Flags, Kind, Size,
382 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000383 return false;
384}
385
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000386bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
387 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000388 if (PreviousSection == NULL)
389 return TokError(".previous without corresponding .section");
390 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000391
392 return false;
393}
394
Michael J. Spencere90ea132010-10-09 03:47:55 +0000395/// ParseDirectiveELFType
396/// ::= .type identifier , @attribute
397bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
398 StringRef Name;
399 if (getParser().ParseIdentifier(Name))
400 return TokError("expected identifier in directive");
401
402 // Handle the identifier as the key symbol.
403 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
404
405 if (getLexer().isNot(AsmToken::Comma))
406 return TokError("unexpected token in '.type' directive");
407 Lex();
408
Rafael Espindolad4a35262010-11-12 15:47:08 +0000409 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
410 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000411 Lex();
412
413 StringRef Type;
414 SMLoc TypeLoc;
415
416 TypeLoc = getLexer().getLoc();
417 if (getParser().ParseIdentifier(Type))
418 return TokError("expected symbol type in directive");
419
420 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
421 .Case("function", MCSA_ELF_TypeFunction)
422 .Case("object", MCSA_ELF_TypeObject)
423 .Case("tls_object", MCSA_ELF_TypeTLS)
424 .Case("common", MCSA_ELF_TypeCommon)
425 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000426 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000427 .Default(MCSA_Invalid);
428
429 if (Attr == MCSA_Invalid)
430 return Error(TypeLoc, "unsupported attribute in '.type' directive");
431
432 if (getLexer().isNot(AsmToken::EndOfStatement))
433 return TokError("unexpected token in '.type' directive");
434
435 Lex();
436
437 getStreamer().EmitSymbolAttribute(Sym, Attr);
438
439 return false;
440}
441
Rafael Espindola61e3b912010-10-26 19:35:47 +0000442/// ParseDirectiveIdent
443/// ::= .ident string
444bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
445 if (getLexer().isNot(AsmToken::String))
446 return TokError("unexpected token in '.ident' directive");
447
448 StringRef Data = getTok().getIdentifier();
449
450 Lex();
451
Rafael Espindola61e3b912010-10-26 19:35:47 +0000452 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000453 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000454 ELF::SHF_MERGE |
455 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000456 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000457 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000458
459 static bool First = true;
460
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000461 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000462 getStreamer().SwitchSection(Comment);
463 if (First)
464 getStreamer().EmitIntValue(0, 1);
465 First = false;
466 getStreamer().EmitBytes(Data, 0);
467 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000468 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000469 return false;
470}
471
Rafael Espindola88182132010-10-27 15:18:17 +0000472/// ParseDirectiveSymver
473/// ::= .symver foo, bar2@zed
474bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
475 StringRef Name;
476 if (getParser().ParseIdentifier(Name))
477 return TokError("expected identifier in directive");
478
479 if (getLexer().isNot(AsmToken::Comma))
480 return TokError("expected a comma");
481
482 Lex();
483
484 StringRef AliasName;
485 if (getParser().ParseIdentifier(AliasName))
486 return TokError("expected identifier in directive");
487
488 if (AliasName.find('@') == StringRef::npos)
489 return TokError("expected a '@' in the name");
490
491 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
492 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
493 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
494
495 getStreamer().EmitAssignment(Alias, Value);
496 return false;
497}
498
Rafael Espindola484291c2010-11-01 14:28:48 +0000499/// ParseDirectiveWeakref
500/// ::= .weakref foo, bar
501bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
502 // FIXME: Share code with the other alias building directives.
503
504 StringRef AliasName;
505 if (getParser().ParseIdentifier(AliasName))
506 return TokError("expected identifier in directive");
507
508 if (getLexer().isNot(AsmToken::Comma))
509 return TokError("expected a comma");
510
511 Lex();
512
513 StringRef Name;
514 if (getParser().ParseIdentifier(Name))
515 return TokError("expected identifier in directive");
516
517 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
518
519 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
520
521 getStreamer().EmitWeakReference(Alias, Sym);
522 return false;
523}
524
Daniel Dunbar5146a092010-07-12 21:23:32 +0000525namespace llvm {
526
527MCAsmParserExtension *createELFAsmParser() {
528 return new ELFAsmParser;
529}
530
531}