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