Daniel Dunbar | 5146a09 | 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 | |
| 10 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringSwitch.h" |
Eli Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionELF.h" |
| 18 | #include "llvm/MC/MCStreamer.h" |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 19 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ELF.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class ELFAsmParser : public MCAsmParserExtension { |
Eli Bendersky | 171192f | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 26 | template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)> |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 27 | void addDirectiveHandler(StringRef Directive) { |
Eli Bendersky | 171192f | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 28 | MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair( |
| 29 | this, HandleDirective<ELFAsmParser, HandlerMethod>); |
| 30 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 31 | getParser().addDirectiveHandler(Directive, Handler); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 34 | bool ParseSectionSwitch(StringRef Section, unsigned Type, |
| 35 | unsigned Flags, SectionKind Kind); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 36 | bool SeenIdent; |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 39 | ELFAsmParser() : SeenIdent(false) { |
| 40 | BracketExpressionsSupported = true; |
| 41 | } |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 42 | |
| 43 | virtual void Initialize(MCAsmParser &Parser) { |
| 44 | // Call the base implementation. |
| 45 | this->MCAsmParserExtension::Initialize(Parser); |
| 46 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 47 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data"); |
| 48 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text"); |
| 49 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss"); |
| 50 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata"); |
| 51 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata"); |
| 52 | addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss"); |
| 53 | addDirectiveHandler< |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 54 | &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 55 | addDirectiveHandler< |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 56 | &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 57 | addDirectiveHandler< |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 58 | &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 59 | addDirectiveHandler< |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 60 | &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 61 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); |
| 62 | addDirectiveHandler< |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 63 | &ELFAsmParser::ParseDirectivePushSection>(".pushsection"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 64 | addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection"); |
| 65 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size"); |
| 66 | addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous"); |
| 67 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type"); |
| 68 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident"); |
| 69 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver"); |
| 70 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version"); |
| 71 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref"); |
| 72 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak"); |
| 73 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local"); |
| 74 | addDirectiveHandler< |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 75 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 76 | addDirectiveHandler< |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 77 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 78 | addDirectiveHandler< |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 79 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden"); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 80 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection"); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 83 | // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is |
| 84 | // the best way for us to get access to it? |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 85 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 86 | return ParseSectionSwitch(".data", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 87 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 88 | SectionKind::getDataRel()); |
| 89 | } |
| 90 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 91 | return ParseSectionSwitch(".text", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 92 | ELF::SHF_EXECINSTR | |
| 93 | ELF::SHF_ALLOC, SectionKind::getText()); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 94 | } |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 95 | bool ParseSectionDirectiveBSS(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 96 | return ParseSectionSwitch(".bss", ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 97 | ELF::SHF_WRITE | |
| 98 | ELF::SHF_ALLOC, SectionKind::getBSS()); |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 99 | } |
| 100 | bool ParseSectionDirectiveRoData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 101 | return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 102 | ELF::SHF_ALLOC, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 103 | SectionKind::getReadOnly()); |
| 104 | } |
| 105 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 106 | return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 107 | ELF::SHF_ALLOC | |
| 108 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 109 | SectionKind::getThreadData()); |
| 110 | } |
| 111 | bool ParseSectionDirectiveTBSS(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 112 | return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 113 | ELF::SHF_ALLOC | |
| 114 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 115 | SectionKind::getThreadBSS()); |
| 116 | } |
| 117 | bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 118 | return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 119 | ELF::SHF_ALLOC | |
| 120 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 121 | SectionKind::getDataRel()); |
| 122 | } |
| 123 | bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 124 | return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 125 | ELF::SHF_ALLOC | |
| 126 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 127 | SectionKind::getReadOnlyWithRel()); |
| 128 | } |
| 129 | bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 130 | return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 131 | ELF::SHF_ALLOC | |
| 132 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 133 | SectionKind::getReadOnlyWithRelLocal()); |
| 134 | } |
| 135 | bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 136 | return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 137 | ELF::SHF_ALLOC | |
| 138 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 139 | SectionKind::getDataRel()); |
| 140 | } |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 141 | bool ParseDirectivePushSection(StringRef, SMLoc); |
| 142 | bool ParseDirectivePopSection(StringRef, SMLoc); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 143 | bool ParseDirectiveSection(StringRef, SMLoc); |
| 144 | bool ParseDirectiveSize(StringRef, SMLoc); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 145 | bool ParseDirectivePrevious(StringRef, SMLoc); |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 146 | bool ParseDirectiveType(StringRef, SMLoc); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 147 | bool ParseDirectiveIdent(StringRef, SMLoc); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 148 | bool ParseDirectiveSymver(StringRef, SMLoc); |
Benjamin Kramer | 6a80f9d | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 149 | bool ParseDirectiveVersion(StringRef, SMLoc); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 150 | bool ParseDirectiveWeakref(StringRef, SMLoc); |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 151 | bool ParseDirectiveSymbolAttribute(StringRef, SMLoc); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 152 | bool ParseDirectiveSubsection(StringRef, SMLoc); |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 153 | |
| 154 | private: |
| 155 | bool ParseSectionName(StringRef &SectionName); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 156 | bool ParseSectionArguments(bool IsPush); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } |
| 160 | |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 161 | /// ParseDirectiveSymbolAttribute |
| 162 | /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ] |
| 163 | bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) { |
| 164 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive) |
| 165 | .Case(".weak", MCSA_Weak) |
| 166 | .Case(".local", MCSA_Local) |
| 167 | .Case(".hidden", MCSA_Hidden) |
| 168 | .Case(".internal", MCSA_Internal) |
| 169 | .Case(".protected", MCSA_Protected) |
| 170 | .Default(MCSA_Invalid); |
| 171 | assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!"); |
| 172 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 173 | for (;;) { |
| 174 | StringRef Name; |
| 175 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 176 | if (getParser().parseIdentifier(Name)) |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 177 | return TokError("expected identifier in directive"); |
| 178 | |
| 179 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 180 | |
| 181 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
| 182 | |
| 183 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 184 | break; |
| 185 | |
| 186 | if (getLexer().isNot(AsmToken::Comma)) |
| 187 | return TokError("unexpected token in directive"); |
| 188 | Lex(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | Lex(); |
| 193 | return false; |
| 194 | } |
| 195 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 196 | bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, |
| 197 | unsigned Flags, SectionKind Kind) { |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 198 | const MCExpr *Subsection = 0; |
| 199 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 200 | if (getParser().parseExpression(Subsection)) |
| 201 | return true; |
| 202 | } |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 203 | |
| 204 | getStreamer().SwitchSection(getContext().getELFSection( |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 205 | Section, Type, Flags, Kind), |
| 206 | Subsection); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 211 | bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 212 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 213 | if (getParser().parseIdentifier(Name)) |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 214 | return TokError("expected identifier in directive"); |
Dmitri Gribenko | 2de0572 | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 215 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 216 | |
| 217 | if (getLexer().isNot(AsmToken::Comma)) |
| 218 | return TokError("unexpected token in directive"); |
| 219 | Lex(); |
| 220 | |
| 221 | const MCExpr *Expr; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 222 | if (getParser().parseExpression(Expr)) |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 223 | return true; |
| 224 | |
| 225 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 226 | return TokError("unexpected token in directive"); |
| 227 | |
| 228 | getStreamer().EmitELFSize(Sym, Expr); |
| 229 | return false; |
| 230 | } |
| 231 | |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 232 | bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { |
| 233 | // A section name can contain -, so we cannot just use |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 234 | // parseIdentifier. |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 235 | SMLoc FirstLoc = getLexer().getLoc(); |
| 236 | unsigned Size = 0; |
| 237 | |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 238 | if (getLexer().is(AsmToken::String)) { |
| 239 | SectionName = getTok().getIdentifier(); |
| 240 | Lex(); |
| 241 | return false; |
| 242 | } |
| 243 | |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 244 | for (;;) { |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 245 | unsigned CurSize; |
| 246 | |
| 247 | SMLoc PrevLoc = getLexer().getLoc(); |
| 248 | if (getLexer().is(AsmToken::Minus)) { |
| 249 | CurSize = 1; |
| 250 | Lex(); // Consume the "-". |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 251 | } else if (getLexer().is(AsmToken::String)) { |
| 252 | CurSize = getTok().getIdentifier().size() + 2; |
| 253 | Lex(); |
| 254 | } else if (getLexer().is(AsmToken::Identifier)) { |
| 255 | CurSize = getTok().getIdentifier().size(); |
| 256 | Lex(); |
| 257 | } else { |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 258 | break; |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 259 | } |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 260 | |
| 261 | Size += CurSize; |
| 262 | SectionName = StringRef(FirstLoc.getPointer(), Size); |
| 263 | |
| 264 | // Make sure the following token is adjacent. |
| 265 | if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) |
| 266 | break; |
| 267 | } |
| 268 | if (Size == 0) |
| 269 | return true; |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 274 | static SectionKind computeSectionKind(unsigned Flags) { |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 275 | if (Flags & ELF::SHF_EXECINSTR) |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 276 | return SectionKind::getText(); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 277 | if (Flags & ELF::SHF_TLS) |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 278 | return SectionKind::getThreadData(); |
| 279 | return SectionKind::getDataRel(); |
| 280 | } |
| 281 | |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 282 | static int parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 283 | int flags = 0; |
| 284 | |
| 285 | for (unsigned i = 0; i < flagsStr.size(); i++) { |
| 286 | switch (flagsStr[i]) { |
| 287 | case 'a': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 288 | flags |= ELF::SHF_ALLOC; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 289 | break; |
| 290 | case 'x': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 291 | flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 292 | break; |
| 293 | case 'w': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 294 | flags |= ELF::SHF_WRITE; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 295 | break; |
| 296 | case 'M': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 297 | flags |= ELF::SHF_MERGE; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 298 | break; |
| 299 | case 'S': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 300 | flags |= ELF::SHF_STRINGS; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 301 | break; |
| 302 | case 'T': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 303 | flags |= ELF::SHF_TLS; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 304 | break; |
| 305 | case 'c': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 306 | flags |= ELF::XCORE_SHF_CP_SECTION; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 307 | break; |
| 308 | case 'd': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 309 | flags |= ELF::XCORE_SHF_DP_SECTION; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 310 | break; |
| 311 | case 'G': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 312 | flags |= ELF::SHF_GROUP; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 313 | break; |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 314 | case '?': |
| 315 | *UseLastGroup = true; |
| 316 | break; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 317 | default: |
| 318 | return -1; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return flags; |
| 323 | } |
| 324 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 325 | bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) { |
| 326 | getStreamer().PushSection(); |
| 327 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 328 | if (ParseSectionArguments(/*IsPush=*/true)) { |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 329 | getStreamer().PopSection(); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { |
| 337 | if (!getStreamer().PopSection()) |
| 338 | return TokError(".popsection without corresponding .pushsection"); |
| 339 | return false; |
| 340 | } |
| 341 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 342 | // FIXME: This is a work in progress. |
| 343 | bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 344 | return ParseSectionArguments(/*IsPush=*/false); |
| 345 | } |
| 346 | |
| 347 | bool ELFAsmParser::ParseSectionArguments(bool IsPush) { |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 348 | StringRef SectionName; |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 349 | |
| 350 | if (ParseSectionName(SectionName)) |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 351 | return TokError("expected identifier in directive"); |
| 352 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 353 | StringRef TypeName; |
| 354 | int64_t Size = 0; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 355 | StringRef GroupName; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 356 | unsigned Flags = 0; |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 357 | const MCExpr *Subsection = 0; |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 358 | bool UseLastGroup = false; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 359 | |
| 360 | // Set the defaults first. |
| 361 | if (SectionName == ".fini" || SectionName == ".init" || |
| 362 | SectionName == ".rodata") |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 363 | Flags |= ELF::SHF_ALLOC; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 364 | if (SectionName == ".fini" || SectionName == ".init") |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 365 | Flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 366 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 367 | if (getLexer().is(AsmToken::Comma)) { |
| 368 | Lex(); |
| 369 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 370 | if (IsPush && getLexer().isNot(AsmToken::String)) { |
| 371 | if (getParser().parseExpression(Subsection)) |
| 372 | return true; |
| 373 | if (getLexer().isNot(AsmToken::Comma)) |
| 374 | goto EndStmt; |
| 375 | Lex(); |
| 376 | } |
| 377 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 378 | if (getLexer().isNot(AsmToken::String)) |
| 379 | return TokError("expected string in directive"); |
| 380 | |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 381 | StringRef FlagsStr = getTok().getStringContents(); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 382 | Lex(); |
| 383 | |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 384 | int extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup); |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 385 | if (extraFlags < 0) |
| 386 | return TokError("unknown flag"); |
| 387 | Flags |= extraFlags; |
| 388 | |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 389 | bool Mergeable = Flags & ELF::SHF_MERGE; |
| 390 | bool Group = Flags & ELF::SHF_GROUP; |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 391 | if (Group && UseLastGroup) |
| 392 | return TokError("Section cannot specifiy a group name while also acting " |
| 393 | "as a member of the last group"); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 394 | |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 395 | if (getLexer().isNot(AsmToken::Comma)) { |
| 396 | if (Mergeable) |
| 397 | return TokError("Mergeable section must specify the type"); |
| 398 | if (Group) |
| 399 | return TokError("Group section must specify the type"); |
| 400 | } else { |
| 401 | Lex(); |
Rafael Espindola | d4a3526 | 2010-11-12 15:47:08 +0000 | [diff] [blame] | 402 | if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At)) |
| 403 | return TokError("expected '@' or '%' before type"); |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 404 | |
| 405 | Lex(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 406 | if (getParser().parseIdentifier(TypeName)) |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 407 | return TokError("expected identifier in directive"); |
| 408 | |
| 409 | if (Mergeable) { |
| 410 | if (getLexer().isNot(AsmToken::Comma)) |
| 411 | return TokError("expected the entry size"); |
| 412 | Lex(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 413 | if (getParser().parseAbsoluteExpression(Size)) |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 414 | return true; |
| 415 | if (Size <= 0) |
| 416 | return TokError("entry size must be positive"); |
| 417 | } |
| 418 | |
| 419 | if (Group) { |
| 420 | if (getLexer().isNot(AsmToken::Comma)) |
| 421 | return TokError("expected group name"); |
| 422 | Lex(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 423 | if (getParser().parseIdentifier(GroupName)) |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 424 | return true; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 425 | if (getLexer().is(AsmToken::Comma)) { |
| 426 | Lex(); |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 427 | StringRef Linkage; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 428 | if (getParser().parseIdentifier(Linkage)) |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 429 | return true; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 430 | if (Linkage != "comdat") |
| 431 | return TokError("Linkage must be 'comdat'"); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 437 | EndStmt: |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 438 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 439 | return TokError("unexpected token in directive"); |
| 440 | |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 441 | unsigned Type = ELF::SHT_PROGBITS; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 442 | |
Joerg Sonnenberger | 42edeb1 | 2013-02-16 00:32:53 +0000 | [diff] [blame] | 443 | if (TypeName.empty()) { |
| 444 | if (SectionName.startswith(".note")) |
| 445 | Type = ELF::SHT_NOTE; |
| 446 | else if (SectionName == ".init_array") |
| 447 | Type = ELF::SHT_INIT_ARRAY; |
| 448 | else if (SectionName == ".fini_array") |
| 449 | Type = ELF::SHT_FINI_ARRAY; |
| 450 | else if (SectionName == ".preinit_array") |
| 451 | Type = ELF::SHT_PREINIT_ARRAY; |
| 452 | } else { |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 453 | if (TypeName == "init_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 454 | Type = ELF::SHT_INIT_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 455 | else if (TypeName == "fini_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 456 | Type = ELF::SHT_FINI_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 457 | else if (TypeName == "preinit_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 458 | Type = ELF::SHT_PREINIT_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 459 | else if (TypeName == "nobits") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 460 | Type = ELF::SHT_NOBITS; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 461 | else if (TypeName == "progbits") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 462 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | 9897661 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 463 | else if (TypeName == "note") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 464 | Type = ELF::SHT_NOTE; |
Rafael Espindola | 0cf5e3d | 2011-01-23 05:43:40 +0000 | [diff] [blame] | 465 | else if (TypeName == "unwind") |
| 466 | Type = ELF::SHT_X86_64_UNWIND; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 467 | else |
| 468 | return TokError("unknown section type"); |
| 469 | } |
| 470 | |
David Majnemer | bcd9b3b | 2013-09-15 19:24:16 +0000 | [diff] [blame^] | 471 | if (UseLastGroup) { |
| 472 | MCSectionSubPair CurrentSection = getStreamer().getCurrentSection(); |
| 473 | if (const MCSectionELF *Section = |
| 474 | cast_or_null<MCSectionELF>(CurrentSection.first)) |
| 475 | if (const MCSymbol *Group = Section->getGroup()) { |
| 476 | GroupName = Group->getName(); |
| 477 | Flags |= ELF::SHF_GROUP; |
| 478 | } |
| 479 | } |
| 480 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 481 | SectionKind Kind = computeSectionKind(Flags); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 482 | getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type, |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 483 | Flags, Kind, Size, |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 484 | GroupName), |
| 485 | Subsection); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 489 | bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 490 | MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); |
| 491 | if (PreviousSection.first == NULL) |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 492 | return TokError(".previous without corresponding .section"); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 493 | getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 494 | |
| 495 | return false; |
| 496 | } |
| 497 | |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 498 | /// ParseDirectiveELFType |
| 499 | /// ::= .type identifier , @attribute |
| 500 | bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { |
| 501 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 502 | if (getParser().parseIdentifier(Name)) |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 503 | return TokError("expected identifier in directive"); |
| 504 | |
| 505 | // Handle the identifier as the key symbol. |
| 506 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 507 | |
| 508 | if (getLexer().isNot(AsmToken::Comma)) |
| 509 | return TokError("unexpected token in '.type' directive"); |
| 510 | Lex(); |
| 511 | |
Rafael Espindola | d4a3526 | 2010-11-12 15:47:08 +0000 | [diff] [blame] | 512 | if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At)) |
| 513 | return TokError("expected '@' or '%' before type"); |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 514 | Lex(); |
| 515 | |
| 516 | StringRef Type; |
| 517 | SMLoc TypeLoc; |
| 518 | |
| 519 | TypeLoc = getLexer().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 520 | if (getParser().parseIdentifier(Type)) |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 521 | return TokError("expected symbol type in directive"); |
| 522 | |
| 523 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type) |
| 524 | .Case("function", MCSA_ELF_TypeFunction) |
| 525 | .Case("object", MCSA_ELF_TypeObject) |
| 526 | .Case("tls_object", MCSA_ELF_TypeTLS) |
| 527 | .Case("common", MCSA_ELF_TypeCommon) |
| 528 | .Case("notype", MCSA_ELF_TypeNoType) |
Rafael Espindola | e13a0ff | 2010-11-13 04:51:02 +0000 | [diff] [blame] | 529 | .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) |
Roman Divacky | a0c17a4 | 2011-12-12 17:34:04 +0000 | [diff] [blame] | 530 | .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction) |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 531 | .Default(MCSA_Invalid); |
| 532 | |
| 533 | if (Attr == MCSA_Invalid) |
| 534 | return Error(TypeLoc, "unsupported attribute in '.type' directive"); |
| 535 | |
| 536 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 537 | return TokError("unexpected token in '.type' directive"); |
| 538 | |
| 539 | Lex(); |
| 540 | |
| 541 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
| 542 | |
| 543 | return false; |
| 544 | } |
| 545 | |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 546 | /// ParseDirectiveIdent |
| 547 | /// ::= .ident string |
| 548 | bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { |
| 549 | if (getLexer().isNot(AsmToken::String)) |
| 550 | return TokError("unexpected token in '.ident' directive"); |
| 551 | |
| 552 | StringRef Data = getTok().getIdentifier(); |
| 553 | |
| 554 | Lex(); |
| 555 | |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 556 | const MCSection *Comment = |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 557 | getContext().getELFSection(".comment", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 558 | ELF::SHF_MERGE | |
| 559 | ELF::SHF_STRINGS, |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 560 | SectionKind::getReadOnly(), |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 561 | 1, ""); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 562 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 563 | getStreamer().PushSection(); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 564 | getStreamer().SwitchSection(Comment); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 565 | if (!SeenIdent) { |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 566 | getStreamer().EmitIntValue(0, 1); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 567 | SeenIdent = true; |
| 568 | } |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame] | 569 | getStreamer().EmitBytes(Data); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 570 | getStreamer().EmitIntValue(0, 1); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 571 | getStreamer().PopSection(); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 572 | return false; |
| 573 | } |
| 574 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 575 | /// ParseDirectiveSymver |
| 576 | /// ::= .symver foo, bar2@zed |
| 577 | bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { |
| 578 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 579 | if (getParser().parseIdentifier(Name)) |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 580 | return TokError("expected identifier in directive"); |
| 581 | |
| 582 | if (getLexer().isNot(AsmToken::Comma)) |
| 583 | return TokError("expected a comma"); |
| 584 | |
| 585 | Lex(); |
| 586 | |
| 587 | StringRef AliasName; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 588 | if (getParser().parseIdentifier(AliasName)) |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 589 | return TokError("expected identifier in directive"); |
| 590 | |
| 591 | if (AliasName.find('@') == StringRef::npos) |
| 592 | return TokError("expected a '@' in the name"); |
| 593 | |
| 594 | MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); |
| 595 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 596 | const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext()); |
| 597 | |
| 598 | getStreamer().EmitAssignment(Alias, Value); |
| 599 | return false; |
| 600 | } |
| 601 | |
Benjamin Kramer | 6a80f9d | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 602 | /// ParseDirectiveVersion |
| 603 | /// ::= .version string |
| 604 | bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { |
| 605 | if (getLexer().isNot(AsmToken::String)) |
| 606 | return TokError("unexpected token in '.version' directive"); |
| 607 | |
| 608 | StringRef Data = getTok().getIdentifier(); |
| 609 | |
| 610 | Lex(); |
| 611 | |
| 612 | const MCSection *Note = |
| 613 | getContext().getELFSection(".note", ELF::SHT_NOTE, 0, |
| 614 | SectionKind::getReadOnly()); |
| 615 | |
| 616 | getStreamer().PushSection(); |
| 617 | getStreamer().SwitchSection(Note); |
| 618 | getStreamer().EmitIntValue(Data.size()+1, 4); // namesz. |
| 619 | getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description). |
| 620 | getStreamer().EmitIntValue(1, 4); // type = NT_VERSION. |
Eric Christopher | 68ca562 | 2013-01-09 01:57:54 +0000 | [diff] [blame] | 621 | getStreamer().EmitBytes(Data); // name. |
Benjamin Kramer | 6a80f9d | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 622 | getStreamer().EmitIntValue(0, 1); // terminate the string. |
| 623 | getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment. |
| 624 | getStreamer().PopSection(); |
| 625 | return false; |
| 626 | } |
| 627 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 628 | /// ParseDirectiveWeakref |
| 629 | /// ::= .weakref foo, bar |
| 630 | bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { |
| 631 | // FIXME: Share code with the other alias building directives. |
| 632 | |
| 633 | StringRef AliasName; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 634 | if (getParser().parseIdentifier(AliasName)) |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 635 | return TokError("expected identifier in directive"); |
| 636 | |
| 637 | if (getLexer().isNot(AsmToken::Comma)) |
| 638 | return TokError("expected a comma"); |
| 639 | |
| 640 | Lex(); |
| 641 | |
| 642 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 643 | if (getParser().parseIdentifier(Name)) |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 644 | return TokError("expected identifier in directive"); |
| 645 | |
| 646 | MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); |
| 647 | |
| 648 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 649 | |
| 650 | getStreamer().EmitWeakReference(Alias, Sym); |
| 651 | return false; |
| 652 | } |
| 653 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 654 | bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) { |
| 655 | const MCExpr *Subsection = 0; |
| 656 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 657 | if (getParser().parseExpression(Subsection)) |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 662 | return TokError("unexpected token in directive"); |
| 663 | |
| 664 | getStreamer().SubSection(Subsection); |
| 665 | return false; |
| 666 | } |
| 667 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 668 | namespace llvm { |
| 669 | |
| 670 | MCAsmParserExtension *createELFAsmParser() { |
| 671 | return new ELFAsmParser; |
| 672 | } |
| 673 | |
| 674 | } |