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