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