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

source code of llvm/lib/MC/MCParser/ELFAsmParser.cpp