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