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