1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/StringSwitch.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDirectives.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCParser/MCAsmParser.h"
18 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/SectionKind.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/SMLoc.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <utility>
31
32 using namespace llvm;
33
34 namespace {
35
36 class ELFAsmParser : public MCAsmParserExtension {
37 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
addDirectiveHandler(StringRef Directive)38 void addDirectiveHandler(StringRef Directive) {
39 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
40 this, HandleDirective<ELFAsmParser, HandlerMethod>);
41
42 getParser().addDirectiveHandler(Directive, Handler);
43 }
44
45 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
46 SectionKind Kind);
47
48 public:
ELFAsmParser()49 ELFAsmParser() { BracketExpressionsSupported = true; }
50
Initialize(MCAsmParser & Parser)51 void Initialize(MCAsmParser &Parser) override {
52 // Call the base implementation.
53 this->MCAsmParserExtension::Initialize(Parser);
54
55 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
61 addDirectiveHandler<
62 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
63 addDirectiveHandler<
64 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
65 addDirectiveHandler<
66 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
68 addDirectiveHandler<
69 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
80 addDirectiveHandler<
81 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
82 addDirectiveHandler<
83 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
84 addDirectiveHandler<
85 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
86 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
87 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
88 }
89
90 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
91 // the best way for us to get access to it?
ParseSectionDirectiveData(StringRef,SMLoc)92 bool ParseSectionDirectiveData(StringRef, SMLoc) {
93 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
94 ELF::SHF_WRITE | ELF::SHF_ALLOC,
95 SectionKind::getData());
96 }
ParseSectionDirectiveText(StringRef,SMLoc)97 bool ParseSectionDirectiveText(StringRef, SMLoc) {
98 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
99 ELF::SHF_EXECINSTR |
100 ELF::SHF_ALLOC, SectionKind::getText());
101 }
ParseSectionDirectiveBSS(StringRef,SMLoc)102 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
103 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
104 ELF::SHF_WRITE |
105 ELF::SHF_ALLOC, SectionKind::getBSS());
106 }
ParseSectionDirectiveRoData(StringRef,SMLoc)107 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
108 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
109 ELF::SHF_ALLOC,
110 SectionKind::getReadOnly());
111 }
ParseSectionDirectiveTData(StringRef,SMLoc)112 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
113 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
114 ELF::SHF_ALLOC |
115 ELF::SHF_TLS | ELF::SHF_WRITE,
116 SectionKind::getThreadData());
117 }
ParseSectionDirectiveTBSS(StringRef,SMLoc)118 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
119 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
120 ELF::SHF_ALLOC |
121 ELF::SHF_TLS | ELF::SHF_WRITE,
122 SectionKind::getThreadBSS());
123 }
ParseSectionDirectiveDataRel(StringRef,SMLoc)124 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
125 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
126 ELF::SHF_ALLOC | ELF::SHF_WRITE,
127 SectionKind::getData());
128 }
ParseSectionDirectiveDataRelRo(StringRef,SMLoc)129 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
130 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
131 ELF::SHF_ALLOC |
132 ELF::SHF_WRITE,
133 SectionKind::getReadOnlyWithRel());
134 }
ParseSectionDirectiveEhFrame(StringRef,SMLoc)135 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
137 ELF::SHF_ALLOC | ELF::SHF_WRITE,
138 SectionKind::getData());
139 }
140 bool ParseDirectivePushSection(StringRef, SMLoc);
141 bool ParseDirectivePopSection(StringRef, SMLoc);
142 bool ParseDirectiveSection(StringRef, SMLoc);
143 bool ParseDirectiveSize(StringRef, SMLoc);
144 bool ParseDirectivePrevious(StringRef, SMLoc);
145 bool ParseDirectiveType(StringRef, SMLoc);
146 bool ParseDirectiveIdent(StringRef, SMLoc);
147 bool ParseDirectiveSymver(StringRef, SMLoc);
148 bool ParseDirectiveVersion(StringRef, SMLoc);
149 bool ParseDirectiveWeakref(StringRef, SMLoc);
150 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
151 bool ParseDirectiveSubsection(StringRef, SMLoc);
152 bool ParseDirectiveCGProfile(StringRef, SMLoc);
153
154 private:
155 bool ParseSectionName(StringRef &SectionName);
156 bool ParseSectionArguments(bool IsPush, SMLoc loc);
157 unsigned parseSunStyleSectionFlags();
158 bool maybeParseSectionType(StringRef &TypeName);
159 bool parseMergeSize(int64_t &Size);
160 bool parseGroup(StringRef &GroupName, bool &IsComdat);
161 bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
162 bool maybeParseUniqueID(int64_t &UniqueID);
163 };
164
165 } // end anonymous namespace
166
167 /// ParseDirectiveSymbolAttribute
168 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
ParseDirectiveSymbolAttribute(StringRef Directive,SMLoc)169 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
170 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
171 .Case(".weak", MCSA_Weak)
172 .Case(".local", MCSA_Local)
173 .Case(".hidden", MCSA_Hidden)
174 .Case(".internal", MCSA_Internal)
175 .Case(".protected", MCSA_Protected)
176 .Default(MCSA_Invalid);
177 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
178 if (getLexer().isNot(AsmToken::EndOfStatement)) {
179 while (true) {
180 StringRef Name;
181
182 if (getParser().parseIdentifier(Name))
183 return TokError("expected identifier in directive");
184
185 if (getParser().discardLTOSymbol(Name)) {
186 if (getLexer().is(AsmToken::EndOfStatement))
187 break;
188 continue;
189 }
190
191 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
192
193 getStreamer().emitSymbolAttribute(Sym, Attr);
194
195 if (getLexer().is(AsmToken::EndOfStatement))
196 break;
197
198 if (getLexer().isNot(AsmToken::Comma))
199 return TokError("unexpected token in directive");
200 Lex();
201 }
202 }
203
204 Lex();
205 return false;
206 }
207
ParseSectionSwitch(StringRef Section,unsigned Type,unsigned Flags,SectionKind Kind)208 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
209 unsigned Flags, SectionKind Kind) {
210 const MCExpr *Subsection = nullptr;
211 if (getLexer().isNot(AsmToken::EndOfStatement)) {
212 if (getParser().parseExpression(Subsection))
213 return true;
214 }
215 Lex();
216
217 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
218 Subsection);
219
220 return false;
221 }
222
ParseDirectiveSize(StringRef,SMLoc)223 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
224 StringRef Name;
225 if (getParser().parseIdentifier(Name))
226 return TokError("expected identifier in directive");
227 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
228
229 if (getLexer().isNot(AsmToken::Comma))
230 return TokError("unexpected token in directive");
231 Lex();
232
233 const MCExpr *Expr;
234 if (getParser().parseExpression(Expr))
235 return true;
236
237 if (getLexer().isNot(AsmToken::EndOfStatement))
238 return TokError("unexpected token in directive");
239 Lex();
240
241 getStreamer().emitELFSize(Sym, Expr);
242 return false;
243 }
244
ParseSectionName(StringRef & SectionName)245 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
246 // A section name can contain -, so we cannot just use
247 // parseIdentifier.
248 SMLoc FirstLoc = getLexer().getLoc();
249 unsigned Size = 0;
250
251 if (getLexer().is(AsmToken::String)) {
252 SectionName = getTok().getIdentifier();
253 Lex();
254 return false;
255 }
256
257 while (!getParser().hasPendingError()) {
258 SMLoc PrevLoc = getLexer().getLoc();
259 if (getLexer().is(AsmToken::Comma) ||
260 getLexer().is(AsmToken::EndOfStatement))
261 break;
262
263 unsigned CurSize;
264 if (getLexer().is(AsmToken::String)) {
265 CurSize = getTok().getIdentifier().size() + 2;
266 Lex();
267 } else if (getLexer().is(AsmToken::Identifier)) {
268 CurSize = getTok().getIdentifier().size();
269 Lex();
270 } else {
271 CurSize = getTok().getString().size();
272 Lex();
273 }
274 Size += CurSize;
275 SectionName = StringRef(FirstLoc.getPointer(), Size);
276
277 // Make sure the following token is adjacent.
278 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
279 break;
280 }
281 if (Size == 0)
282 return true;
283
284 return false;
285 }
286
parseSectionFlags(StringRef flagsStr,bool * UseLastGroup)287 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
288 unsigned flags = 0;
289
290 // If a valid numerical value is set for the section flag, use it verbatim
291 if (!flagsStr.getAsInteger(0, flags))
292 return flags;
293
294 for (char i : flagsStr) {
295 switch (i) {
296 case 'a':
297 flags |= ELF::SHF_ALLOC;
298 break;
299 case 'e':
300 flags |= ELF::SHF_EXCLUDE;
301 break;
302 case 'x':
303 flags |= ELF::SHF_EXECINSTR;
304 break;
305 case 'w':
306 flags |= ELF::SHF_WRITE;
307 break;
308 case 'o':
309 flags |= ELF::SHF_LINK_ORDER;
310 break;
311 case 'M':
312 flags |= ELF::SHF_MERGE;
313 break;
314 case 'S':
315 flags |= ELF::SHF_STRINGS;
316 break;
317 case 'T':
318 flags |= ELF::SHF_TLS;
319 break;
320 case 'c':
321 flags |= ELF::XCORE_SHF_CP_SECTION;
322 break;
323 case 'd':
324 flags |= ELF::XCORE_SHF_DP_SECTION;
325 break;
326 case 'y':
327 flags |= ELF::SHF_ARM_PURECODE;
328 break;
329 case 's':
330 flags |= ELF::SHF_HEX_GPREL;
331 break;
332 case 'G':
333 flags |= ELF::SHF_GROUP;
334 break;
335 case 'R':
336 flags |= ELF::SHF_GNU_RETAIN;
337 break;
338 case '?':
339 *UseLastGroup = true;
340 break;
341 default:
342 return -1U;
343 }
344 }
345
346 return flags;
347 }
348
parseSunStyleSectionFlags()349 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
350 unsigned flags = 0;
351 while (getLexer().is(AsmToken::Hash)) {
352 Lex(); // Eat the #.
353
354 if (!getLexer().is(AsmToken::Identifier))
355 return -1U;
356
357 StringRef flagId = getTok().getIdentifier();
358 if (flagId == "alloc")
359 flags |= ELF::SHF_ALLOC;
360 else if (flagId == "execinstr")
361 flags |= ELF::SHF_EXECINSTR;
362 else if (flagId == "write")
363 flags |= ELF::SHF_WRITE;
364 else if (flagId == "tls")
365 flags |= ELF::SHF_TLS;
366 else
367 return -1U;
368
369 Lex(); // Eat the flag.
370
371 if (!getLexer().is(AsmToken::Comma))
372 break;
373 Lex(); // Eat the comma.
374 }
375 return flags;
376 }
377
378
ParseDirectivePushSection(StringRef s,SMLoc loc)379 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
380 getStreamer().PushSection();
381
382 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
383 getStreamer().PopSection();
384 return true;
385 }
386
387 return false;
388 }
389
ParseDirectivePopSection(StringRef,SMLoc)390 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
391 if (!getStreamer().PopSection())
392 return TokError(".popsection without corresponding .pushsection");
393 return false;
394 }
395
ParseDirectiveSection(StringRef,SMLoc loc)396 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
397 return ParseSectionArguments(/*IsPush=*/false, loc);
398 }
399
maybeParseSectionType(StringRef & TypeName)400 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
401 MCAsmLexer &L = getLexer();
402 if (L.isNot(AsmToken::Comma))
403 return false;
404 Lex();
405 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
406 L.isNot(AsmToken::String)) {
407 if (L.getAllowAtInIdentifier())
408 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
409 else
410 return TokError("expected '%<type>' or \"<type>\"");
411 }
412 if (!L.is(AsmToken::String))
413 Lex();
414 if (L.is(AsmToken::Integer)) {
415 TypeName = getTok().getString();
416 Lex();
417 } else if (getParser().parseIdentifier(TypeName))
418 return TokError("expected identifier in directive");
419 return false;
420 }
421
parseMergeSize(int64_t & Size)422 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
423 if (getLexer().isNot(AsmToken::Comma))
424 return TokError("expected the entry size");
425 Lex();
426 if (getParser().parseAbsoluteExpression(Size))
427 return true;
428 if (Size <= 0)
429 return TokError("entry size must be positive");
430 return false;
431 }
432
parseGroup(StringRef & GroupName,bool & IsComdat)433 bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) {
434 MCAsmLexer &L = getLexer();
435 if (L.isNot(AsmToken::Comma))
436 return TokError("expected group name");
437 Lex();
438 if (L.is(AsmToken::Integer)) {
439 GroupName = getTok().getString();
440 Lex();
441 } else if (getParser().parseIdentifier(GroupName)) {
442 return TokError("invalid group name");
443 }
444 if (L.is(AsmToken::Comma)) {
445 Lex();
446 StringRef Linkage;
447 if (getParser().parseIdentifier(Linkage))
448 return TokError("invalid linkage");
449 if (Linkage != "comdat")
450 return TokError("Linkage must be 'comdat'");
451 IsComdat = true;
452 } else {
453 IsComdat = false;
454 }
455 return false;
456 }
457
parseLinkedToSym(MCSymbolELF * & LinkedToSym)458 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
459 MCAsmLexer &L = getLexer();
460 if (L.isNot(AsmToken::Comma))
461 return TokError("expected linked-to symbol");
462 Lex();
463 StringRef Name;
464 SMLoc StartLoc = L.getLoc();
465 if (getParser().parseIdentifier(Name)) {
466 if (getParser().getTok().getString() == "0") {
467 getParser().Lex();
468 LinkedToSym = nullptr;
469 return false;
470 }
471 return TokError("invalid linked-to symbol");
472 }
473 LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
474 if (!LinkedToSym || !LinkedToSym->isInSection())
475 return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
476 return false;
477 }
478
maybeParseUniqueID(int64_t & UniqueID)479 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
480 MCAsmLexer &L = getLexer();
481 if (L.isNot(AsmToken::Comma))
482 return false;
483 Lex();
484 StringRef UniqueStr;
485 if (getParser().parseIdentifier(UniqueStr))
486 return TokError("expected identifier in directive");
487 if (UniqueStr != "unique")
488 return TokError("expected 'unique'");
489 if (L.isNot(AsmToken::Comma))
490 return TokError("expected commma");
491 Lex();
492 if (getParser().parseAbsoluteExpression(UniqueID))
493 return true;
494 if (UniqueID < 0)
495 return TokError("unique id must be positive");
496 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
497 return TokError("unique id is too large");
498 return false;
499 }
500
hasPrefix(StringRef SectionName,StringRef Prefix)501 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
502 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
503 }
504
ParseSectionArguments(bool IsPush,SMLoc loc)505 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
506 StringRef SectionName;
507
508 if (ParseSectionName(SectionName))
509 return TokError("expected identifier in directive");
510
511 StringRef TypeName;
512 int64_t Size = 0;
513 StringRef GroupName;
514 bool IsComdat = false;
515 unsigned Flags = 0;
516 unsigned extraFlags = 0;
517 const MCExpr *Subsection = nullptr;
518 bool UseLastGroup = false;
519 MCSymbolELF *LinkedToSym = nullptr;
520 int64_t UniqueID = ~0;
521
522 // Set the defaults first.
523 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
524 Flags |= ELF::SHF_ALLOC;
525 else if (SectionName == ".fini" || SectionName == ".init" ||
526 hasPrefix(SectionName, ".text."))
527 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
528 else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
529 hasPrefix(SectionName, ".bss.") ||
530 hasPrefix(SectionName, ".init_array.") ||
531 hasPrefix(SectionName, ".fini_array.") ||
532 hasPrefix(SectionName, ".preinit_array."))
533 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
534 else if (hasPrefix(SectionName, ".tdata.") ||
535 hasPrefix(SectionName, ".tbss."))
536 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
537
538 if (getLexer().is(AsmToken::Comma)) {
539 Lex();
540
541 if (IsPush && getLexer().isNot(AsmToken::String)) {
542 if (getParser().parseExpression(Subsection))
543 return true;
544 if (getLexer().isNot(AsmToken::Comma))
545 goto EndStmt;
546 Lex();
547 }
548
549 if (getLexer().isNot(AsmToken::String)) {
550 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
551 || getLexer().isNot(AsmToken::Hash))
552 return TokError("expected string in directive");
553 extraFlags = parseSunStyleSectionFlags();
554 } else {
555 StringRef FlagsStr = getTok().getStringContents();
556 Lex();
557 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
558 }
559
560 if (extraFlags == -1U)
561 return TokError("unknown flag");
562 Flags |= extraFlags;
563
564 bool Mergeable = Flags & ELF::SHF_MERGE;
565 bool Group = Flags & ELF::SHF_GROUP;
566 if (Group && UseLastGroup)
567 return TokError("Section cannot specifiy a group name while also acting "
568 "as a member of the last group");
569
570 if (maybeParseSectionType(TypeName))
571 return true;
572
573 MCAsmLexer &L = getLexer();
574 if (TypeName.empty()) {
575 if (Mergeable)
576 return TokError("Mergeable section must specify the type");
577 if (Group)
578 return TokError("Group section must specify the type");
579 if (L.isNot(AsmToken::EndOfStatement))
580 return TokError("unexpected token in directive");
581 }
582
583 if (Mergeable)
584 if (parseMergeSize(Size))
585 return true;
586 if (Group)
587 if (parseGroup(GroupName, IsComdat))
588 return true;
589 if (Flags & ELF::SHF_LINK_ORDER)
590 if (parseLinkedToSym(LinkedToSym))
591 return true;
592 if (maybeParseUniqueID(UniqueID))
593 return true;
594 }
595
596 EndStmt:
597 if (getLexer().isNot(AsmToken::EndOfStatement))
598 return TokError("unexpected token in directive");
599 Lex();
600
601 unsigned Type = ELF::SHT_PROGBITS;
602
603 if (TypeName.empty()) {
604 if (SectionName.startswith(".note"))
605 Type = ELF::SHT_NOTE;
606 else if (hasPrefix(SectionName, ".init_array."))
607 Type = ELF::SHT_INIT_ARRAY;
608 else if (hasPrefix(SectionName, ".bss."))
609 Type = ELF::SHT_NOBITS;
610 else if (hasPrefix(SectionName, ".tbss."))
611 Type = ELF::SHT_NOBITS;
612 else if (hasPrefix(SectionName, ".fini_array."))
613 Type = ELF::SHT_FINI_ARRAY;
614 else if (hasPrefix(SectionName, ".preinit_array."))
615 Type = ELF::SHT_PREINIT_ARRAY;
616 } else {
617 if (TypeName == "init_array")
618 Type = ELF::SHT_INIT_ARRAY;
619 else if (TypeName == "fini_array")
620 Type = ELF::SHT_FINI_ARRAY;
621 else if (TypeName == "preinit_array")
622 Type = ELF::SHT_PREINIT_ARRAY;
623 else if (TypeName == "nobits")
624 Type = ELF::SHT_NOBITS;
625 else if (TypeName == "progbits")
626 Type = ELF::SHT_PROGBITS;
627 else if (TypeName == "note")
628 Type = ELF::SHT_NOTE;
629 else if (TypeName == "unwind")
630 Type = ELF::SHT_X86_64_UNWIND;
631 else if (TypeName == "llvm_odrtab")
632 Type = ELF::SHT_LLVM_ODRTAB;
633 else if (TypeName == "llvm_linker_options")
634 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
635 else if (TypeName == "llvm_call_graph_profile")
636 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
637 else if (TypeName == "llvm_dependent_libraries")
638 Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES;
639 else if (TypeName == "llvm_sympart")
640 Type = ELF::SHT_LLVM_SYMPART;
641 else if (TypeName == "llvm_bb_addr_map")
642 Type = ELF::SHT_LLVM_BB_ADDR_MAP;
643 else if (TypeName.getAsInteger(0, Type))
644 return TokError("unknown section type");
645 }
646
647 if (UseLastGroup) {
648 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
649 if (const MCSectionELF *Section =
650 cast_or_null<MCSectionELF>(CurrentSection.first))
651 if (const MCSymbol *Group = Section->getGroup()) {
652 GroupName = Group->getName();
653 IsComdat = Section->isComdat();
654 Flags |= ELF::SHF_GROUP;
655 }
656 }
657
658 MCSectionELF *Section =
659 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
660 IsComdat, UniqueID, LinkedToSym);
661 getStreamer().SwitchSection(Section, Subsection);
662 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
663 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't error
664 // for SHT_PROGBITS .eh_frame
665 if (Section->getType() != Type &&
666 !(SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS))
667 Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
668 utohexstr(Section->getType()));
669 // Check that flags are used consistently. However, the GNU assembler permits
670 // to leave out in subsequent uses of the same sections; for compatibility,
671 // do likewise.
672 if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
673 Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
674 utohexstr(Section->getFlags()));
675 if ((extraFlags || Size || !TypeName.empty()) &&
676 Section->getEntrySize() != Size)
677 Error(loc, "changed section entsize for " + SectionName +
678 ", expected: " + Twine(Section->getEntrySize()));
679
680 if (getContext().getGenDwarfForAssembly() &&
681 (Section->getFlags() & ELF::SHF_ALLOC) &&
682 (Section->getFlags() & ELF::SHF_EXECINSTR)) {
683 bool InsertResult = getContext().addGenDwarfSection(Section);
684 if (InsertResult) {
685 if (getContext().getDwarfVersion() <= 2)
686 Warning(loc, "DWARF2 only supports one section per compilation unit");
687
688 if (!Section->getBeginSymbol()) {
689 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
690 getStreamer().emitLabel(SectionStartSymbol);
691 Section->setBeginSymbol(SectionStartSymbol);
692 }
693 }
694 }
695
696 return false;
697 }
698
ParseDirectivePrevious(StringRef DirName,SMLoc)699 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
700 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
701 if (PreviousSection.first == nullptr)
702 return TokError(".previous without corresponding .section");
703 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
704
705 return false;
706 }
707
MCAttrForString(StringRef Type)708 static MCSymbolAttr MCAttrForString(StringRef Type) {
709 return StringSwitch<MCSymbolAttr>(Type)
710 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
711 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
712 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
713 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
714 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
715 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
716 MCSA_ELF_TypeIndFunction)
717 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
718 .Default(MCSA_Invalid);
719 }
720
721 /// ParseDirectiveELFType
722 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
723 /// ::= .type identifier , #attribute
724 /// ::= .type identifier , @attribute
725 /// ::= .type identifier , %attribute
726 /// ::= .type identifier , "attribute"
ParseDirectiveType(StringRef,SMLoc)727 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
728 StringRef Name;
729 if (getParser().parseIdentifier(Name))
730 return TokError("expected identifier in directive");
731
732 // Handle the identifier as the key symbol.
733 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
734
735 // NOTE the comma is optional in all cases. It is only documented as being
736 // optional in the first case, however, GAS will silently treat the comma as
737 // optional in all cases. Furthermore, although the documentation states that
738 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
739 // accepts both the upper case name as well as the lower case aliases.
740 if (getLexer().is(AsmToken::Comma))
741 Lex();
742
743 if (getLexer().isNot(AsmToken::Identifier) &&
744 getLexer().isNot(AsmToken::Hash) &&
745 getLexer().isNot(AsmToken::Percent) &&
746 getLexer().isNot(AsmToken::String)) {
747 if (!getLexer().getAllowAtInIdentifier())
748 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
749 "'%<type>' or \"<type>\"");
750 else if (getLexer().isNot(AsmToken::At))
751 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
752 "'%<type>' or \"<type>\"");
753 }
754
755 if (getLexer().isNot(AsmToken::String) &&
756 getLexer().isNot(AsmToken::Identifier))
757 Lex();
758
759 SMLoc TypeLoc = getLexer().getLoc();
760
761 StringRef Type;
762 if (getParser().parseIdentifier(Type))
763 return TokError("expected symbol type in directive");
764
765 MCSymbolAttr Attr = MCAttrForString(Type);
766 if (Attr == MCSA_Invalid)
767 return Error(TypeLoc, "unsupported attribute in '.type' directive");
768
769 if (getLexer().isNot(AsmToken::EndOfStatement))
770 return TokError("unexpected token in '.type' directive");
771 Lex();
772
773 getStreamer().emitSymbolAttribute(Sym, Attr);
774
775 return false;
776 }
777
778 /// ParseDirectiveIdent
779 /// ::= .ident string
ParseDirectiveIdent(StringRef,SMLoc)780 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
781 if (getLexer().isNot(AsmToken::String))
782 return TokError("unexpected token in '.ident' directive");
783
784 StringRef Data = getTok().getIdentifier();
785
786 Lex();
787
788 if (getLexer().isNot(AsmToken::EndOfStatement))
789 return TokError("unexpected token in '.ident' directive");
790 Lex();
791
792 getStreamer().emitIdent(Data);
793 return false;
794 }
795
796 /// ParseDirectiveSymver
797 /// ::= .symver foo, bar2@zed
ParseDirectiveSymver(StringRef,SMLoc)798 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
799 StringRef OriginalName, Name, Action;
800 if (getParser().parseIdentifier(OriginalName))
801 return TokError("expected identifier in directive");
802
803 if (getLexer().isNot(AsmToken::Comma))
804 return TokError("expected a comma");
805
806 // ARM assembly uses @ for a comment...
807 // except when parsing the second parameter of the .symver directive.
808 // Force the next symbol to allow @ in the identifier, which is
809 // required for this directive and then reset it to its initial state.
810 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
811 getLexer().setAllowAtInIdentifier(true);
812 Lex();
813 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
814
815 if (getParser().parseIdentifier(Name))
816 return TokError("expected identifier in directive");
817
818 if (Name.find('@') == StringRef::npos)
819 return TokError("expected a '@' in the name");
820 bool KeepOriginalSym = !Name.contains("@@@");
821 if (parseOptionalToken(AsmToken::Comma)) {
822 if (getParser().parseIdentifier(Action) || Action != "remove")
823 return TokError("expected 'remove'");
824 KeepOriginalSym = false;
825 }
826 (void)parseOptionalToken(AsmToken::EndOfStatement);
827
828 getStreamer().emitELFSymverDirective(
829 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
830 return false;
831 }
832
833 /// ParseDirectiveVersion
834 /// ::= .version string
ParseDirectiveVersion(StringRef,SMLoc)835 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
836 if (getLexer().isNot(AsmToken::String))
837 return TokError("unexpected token in '.version' directive");
838
839 StringRef Data = getTok().getIdentifier();
840
841 Lex();
842
843 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
844
845 getStreamer().PushSection();
846 getStreamer().SwitchSection(Note);
847 getStreamer().emitInt32(Data.size() + 1); // namesz
848 getStreamer().emitInt32(0); // descsz = 0 (no description).
849 getStreamer().emitInt32(1); // type = NT_VERSION
850 getStreamer().emitBytes(Data); // name
851 getStreamer().emitInt8(0); // NUL
852 getStreamer().emitValueToAlignment(4);
853 getStreamer().PopSection();
854 return false;
855 }
856
857 /// ParseDirectiveWeakref
858 /// ::= .weakref foo, bar
ParseDirectiveWeakref(StringRef,SMLoc)859 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
860 // FIXME: Share code with the other alias building directives.
861
862 StringRef AliasName;
863 if (getParser().parseIdentifier(AliasName))
864 return TokError("expected identifier in directive");
865
866 if (getLexer().isNot(AsmToken::Comma))
867 return TokError("expected a comma");
868
869 Lex();
870
871 StringRef Name;
872 if (getParser().parseIdentifier(Name))
873 return TokError("expected identifier in directive");
874
875 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
876
877 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
878
879 getStreamer().emitWeakReference(Alias, Sym);
880 return false;
881 }
882
ParseDirectiveSubsection(StringRef,SMLoc)883 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
884 const MCExpr *Subsection = nullptr;
885 if (getLexer().isNot(AsmToken::EndOfStatement)) {
886 if (getParser().parseExpression(Subsection))
887 return true;
888 }
889
890 if (getLexer().isNot(AsmToken::EndOfStatement))
891 return TokError("unexpected token in directive");
892
893 Lex();
894
895 getStreamer().SubSection(Subsection);
896 return false;
897 }
898
ParseDirectiveCGProfile(StringRef S,SMLoc Loc)899 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {
900 return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc);
901 }
902
903 namespace llvm {
904
createELFAsmParser()905 MCAsmParserExtension *createELFAsmParser() {
906 return new ELFAsmParser;
907 }
908
909 } // end namespace llvm
910