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