Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 1 | //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringSwitch.h" |
Eli Friedman | 9de5967 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionELF.h" |
| 18 | #include "llvm/MC/MCStreamer.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSymbolELF.h" |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ELF.h" |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class ELFAsmParser : public MCAsmParserExtension { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 26 | template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)> |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 27 | void addDirectiveHandler(StringRef Directive) { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 28 | MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair( |
| 29 | this, HandleDirective<ELFAsmParser, HandlerMethod>); |
| 30 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 31 | getParser().addDirectiveHandler(Directive, Handler); |
Daniel Dunbar | 8897d47 | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 34 | bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags, |
| 35 | SectionKind Kind); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 38 | ELFAsmParser() { BracketExpressionsSupported = true; } |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 39 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 40 | void Initialize(MCAsmParser &Parser) override { |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 41 | // Call the base implementation. |
| 42 | this->MCAsmParserExtension::Initialize(Parser); |
| 43 | |
Jim Grosbach | d2037eb | 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 | 1a55fa6 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 51 | &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 52 | addDirectiveHandler< |
Jim Grosbach | 1a55fa6 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 53 | &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 54 | addDirectiveHandler< |
Jim Grosbach | 1a55fa6 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 55 | &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 56 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); |
| 57 | addDirectiveHandler< |
Jim Grosbach | 1a55fa6 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 58 | &ELFAsmParser::ParseDirectivePushSection>(".pushsection"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 59 | addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection"); |
| 60 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size"); |
| 61 | addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous"); |
| 62 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type"); |
| 63 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident"); |
| 64 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver"); |
| 65 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version"); |
| 66 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref"); |
| 67 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak"); |
| 68 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local"); |
| 69 | addDirectiveHandler< |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 70 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 71 | addDirectiveHandler< |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 72 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 73 | addDirectiveHandler< |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 74 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden"); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 75 | addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection"); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 78 | // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is |
| 79 | // the best way for us to get access to it? |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 80 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 81 | return ParseSectionSwitch(".data", ELF::SHT_PROGBITS, |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 82 | ELF::SHF_WRITE | ELF::SHF_ALLOC, |
| 83 | SectionKind::getData()); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 84 | } |
| 85 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 86 | return ParseSectionSwitch(".text", ELF::SHT_PROGBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 87 | ELF::SHF_EXECINSTR | |
| 88 | ELF::SHF_ALLOC, SectionKind::getText()); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 89 | } |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 90 | bool ParseSectionDirectiveBSS(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 91 | return ParseSectionSwitch(".bss", ELF::SHT_NOBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 92 | ELF::SHF_WRITE | |
| 93 | ELF::SHF_ALLOC, SectionKind::getBSS()); |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 94 | } |
| 95 | bool ParseSectionDirectiveRoData(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 96 | return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 97 | ELF::SHF_ALLOC, |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 98 | SectionKind::getReadOnly()); |
| 99 | } |
| 100 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 101 | return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 102 | ELF::SHF_ALLOC | |
| 103 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 104 | SectionKind::getThreadData()); |
| 105 | } |
| 106 | bool ParseSectionDirectiveTBSS(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 107 | return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 108 | ELF::SHF_ALLOC | |
| 109 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 110 | SectionKind::getThreadBSS()); |
| 111 | } |
| 112 | bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 113 | return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS, |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 114 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 115 | SectionKind::getData()); |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 116 | } |
| 117 | bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 118 | return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS, |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 119 | ELF::SHF_ALLOC | |
| 120 | ELF::SHF_WRITE, |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 121 | SectionKind::getReadOnlyWithRel()); |
| 122 | } |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 123 | bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 124 | return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS, |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 125 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 126 | SectionKind::getData()); |
Matt Fleming | a8f6c1c | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 127 | } |
Rafael Espindola | 58ac6e1 | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 128 | bool ParseDirectivePushSection(StringRef, SMLoc); |
| 129 | bool ParseDirectivePopSection(StringRef, SMLoc); |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 130 | bool ParseDirectiveSection(StringRef, SMLoc); |
| 131 | bool ParseDirectiveSize(StringRef, SMLoc); |
Benjamin Kramer | e39017c | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 132 | bool ParseDirectivePrevious(StringRef, SMLoc); |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 133 | bool ParseDirectiveType(StringRef, SMLoc); |
Rafael Espindola | c9fb35e | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 134 | bool ParseDirectiveIdent(StringRef, SMLoc); |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 135 | bool ParseDirectiveSymver(StringRef, SMLoc); |
Benjamin Kramer | 6bee7f7 | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 136 | bool ParseDirectiveVersion(StringRef, SMLoc); |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 137 | bool ParseDirectiveWeakref(StringRef, SMLoc); |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 138 | bool ParseDirectiveSymbolAttribute(StringRef, SMLoc); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 139 | bool ParseDirectiveSubsection(StringRef, SMLoc); |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 140 | |
| 141 | private: |
| 142 | bool ParseSectionName(StringRef &SectionName); |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 143 | bool ParseSectionArguments(bool IsPush, SMLoc loc); |
Venkatraman Govindaraju | bf70566 | 2014-03-01 06:21:00 +0000 | [diff] [blame] | 144 | unsigned parseSunStyleSectionFlags(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 147 | } |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 148 | |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 149 | /// ParseDirectiveSymbolAttribute |
| 150 | /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ] |
| 151 | bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) { |
| 152 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive) |
| 153 | .Case(".weak", MCSA_Weak) |
| 154 | .Case(".local", MCSA_Local) |
| 155 | .Case(".hidden", MCSA_Hidden) |
| 156 | .Case(".internal", MCSA_Internal) |
| 157 | .Case(".protected", MCSA_Protected) |
| 158 | .Default(MCSA_Invalid); |
| 159 | assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!"); |
| 160 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 161 | for (;;) { |
| 162 | StringRef Name; |
| 163 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 164 | if (getParser().parseIdentifier(Name)) |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 165 | return TokError("expected identifier in directive"); |
| 166 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 167 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Jim Grosbach | 38b1ed8 | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 168 | |
| 169 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
| 170 | |
| 171 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 172 | break; |
| 173 | |
| 174 | if (getLexer().isNot(AsmToken::Comma)) |
| 175 | return TokError("unexpected token in directive"); |
| 176 | Lex(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | Lex(); |
| 181 | return false; |
| 182 | } |
| 183 | |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 184 | bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, |
| 185 | unsigned Flags, SectionKind Kind) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 186 | const MCExpr *Subsection = nullptr; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 187 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 188 | if (getParser().parseExpression(Subsection)) |
| 189 | return true; |
| 190 | } |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 191 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 192 | getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags), |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 193 | Subsection); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 198 | bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { |
Eli Friedman | 56178a0 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 199 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 200 | if (getParser().parseIdentifier(Name)) |
Eli Friedman | 56178a0 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 201 | return TokError("expected identifier in directive"); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 202 | MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name)); |
Eli Friedman | 56178a0 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 203 | |
| 204 | if (getLexer().isNot(AsmToken::Comma)) |
| 205 | return TokError("unexpected token in directive"); |
| 206 | Lex(); |
| 207 | |
| 208 | const MCExpr *Expr; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 209 | if (getParser().parseExpression(Expr)) |
Eli Friedman | 56178a0 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 210 | return true; |
| 211 | |
| 212 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 213 | return TokError("unexpected token in directive"); |
| 214 | |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 215 | getStreamer().emitELFSize(Sym, Expr); |
Eli Friedman | 56178a0 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 219 | bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { |
| 220 | // A section name can contain -, so we cannot just use |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 221 | // parseIdentifier. |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 222 | SMLoc FirstLoc = getLexer().getLoc(); |
| 223 | unsigned Size = 0; |
| 224 | |
Rafael Espindola | 689939e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 225 | if (getLexer().is(AsmToken::String)) { |
| 226 | SectionName = getTok().getIdentifier(); |
| 227 | Lex(); |
| 228 | return false; |
| 229 | } |
| 230 | |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 231 | for (;;) { |
Marina Yatsina | 33ef7da | 2016-03-22 11:23:15 +0000 | [diff] [blame^] | 232 | |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 233 | SMLoc PrevLoc = getLexer().getLoc(); |
Marina Yatsina | 33ef7da | 2016-03-22 11:23:15 +0000 | [diff] [blame^] | 234 | if (getLexer().is(AsmToken::Comma) || |
| 235 | getLexer().is(AsmToken::EndOfStatement)) |
| 236 | break; |
| 237 | |
| 238 | unsigned CurSize; |
| 239 | if (getLexer().is(AsmToken::String)) { |
Rafael Espindola | 689939e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 240 | CurSize = getTok().getIdentifier().size() + 2; |
| 241 | Lex(); |
| 242 | } else if (getLexer().is(AsmToken::Identifier)) { |
| 243 | CurSize = getTok().getIdentifier().size(); |
| 244 | Lex(); |
| 245 | } else { |
Marina Yatsina | 33ef7da | 2016-03-22 11:23:15 +0000 | [diff] [blame^] | 246 | CurSize = getTok().getString().size(); |
| 247 | Lex(); |
Rafael Espindola | 689939e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 248 | } |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 249 | Size += CurSize; |
| 250 | SectionName = StringRef(FirstLoc.getPointer(), Size); |
| 251 | |
| 252 | // Make sure the following token is adjacent. |
| 253 | if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) |
| 254 | break; |
| 255 | } |
| 256 | if (Size == 0) |
| 257 | return true; |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
Benjamin Kramer | ac511ca | 2013-09-15 19:53:20 +0000 | [diff] [blame] | 262 | static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { |
| 263 | unsigned flags = 0; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 264 | |
| 265 | for (unsigned i = 0; i < flagsStr.size(); i++) { |
| 266 | switch (flagsStr[i]) { |
| 267 | case 'a': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 268 | flags |= ELF::SHF_ALLOC; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 269 | break; |
Benjamin Kramer | ac511ca | 2013-09-15 19:53:20 +0000 | [diff] [blame] | 270 | case 'e': |
| 271 | flags |= ELF::SHF_EXCLUDE; |
| 272 | break; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 273 | case 'x': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 274 | flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 275 | break; |
| 276 | case 'w': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 277 | flags |= ELF::SHF_WRITE; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 'M': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 280 | flags |= ELF::SHF_MERGE; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 281 | break; |
| 282 | case 'S': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 283 | flags |= ELF::SHF_STRINGS; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 284 | break; |
| 285 | case 'T': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 286 | flags |= ELF::SHF_TLS; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 287 | break; |
| 288 | case 'c': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 289 | flags |= ELF::XCORE_SHF_CP_SECTION; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 290 | break; |
| 291 | case 'd': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 292 | flags |= ELF::XCORE_SHF_DP_SECTION; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 293 | break; |
| 294 | case 'G': |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 295 | flags |= ELF::SHF_GROUP; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 296 | break; |
David Majnemer | a4b521b | 2013-09-15 19:24:16 +0000 | [diff] [blame] | 297 | case '?': |
| 298 | *UseLastGroup = true; |
| 299 | break; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 300 | default: |
Benjamin Kramer | ac511ca | 2013-09-15 19:53:20 +0000 | [diff] [blame] | 301 | return -1U; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | return flags; |
| 306 | } |
| 307 | |
Venkatraman Govindaraju | bf70566 | 2014-03-01 06:21:00 +0000 | [diff] [blame] | 308 | unsigned ELFAsmParser::parseSunStyleSectionFlags() { |
| 309 | unsigned flags = 0; |
| 310 | while (getLexer().is(AsmToken::Hash)) { |
| 311 | Lex(); // Eat the #. |
| 312 | |
| 313 | if (!getLexer().is(AsmToken::Identifier)) |
| 314 | return -1U; |
| 315 | |
| 316 | StringRef flagId = getTok().getIdentifier(); |
| 317 | if (flagId == "alloc") |
| 318 | flags |= ELF::SHF_ALLOC; |
| 319 | else if (flagId == "execinstr") |
| 320 | flags |= ELF::SHF_EXECINSTR; |
| 321 | else if (flagId == "write") |
| 322 | flags |= ELF::SHF_WRITE; |
| 323 | else if (flagId == "tls") |
| 324 | flags |= ELF::SHF_TLS; |
| 325 | else |
| 326 | return -1U; |
| 327 | |
| 328 | Lex(); // Eat the flag. |
| 329 | |
| 330 | if (!getLexer().is(AsmToken::Comma)) |
| 331 | break; |
| 332 | Lex(); // Eat the comma. |
| 333 | } |
| 334 | return flags; |
| 335 | } |
| 336 | |
| 337 | |
Rafael Espindola | 58ac6e1 | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 338 | bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) { |
| 339 | getStreamer().PushSection(); |
| 340 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 341 | if (ParseSectionArguments(/*IsPush=*/true, loc)) { |
Rafael Espindola | 58ac6e1 | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 342 | getStreamer().PopSection(); |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { |
| 350 | if (!getStreamer().PopSection()) |
| 351 | return TokError(".popsection without corresponding .pushsection"); |
| 352 | return false; |
| 353 | } |
| 354 | |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 355 | // FIXME: This is a work in progress. |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 356 | bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) { |
| 357 | return ParseSectionArguments(/*IsPush=*/false, loc); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 360 | bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 361 | StringRef SectionName; |
Rafael Espindola | f7f4332 | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 362 | |
| 363 | if (ParseSectionName(SectionName)) |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 364 | return TokError("expected identifier in directive"); |
| 365 | |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 366 | StringRef TypeName; |
| 367 | int64_t Size = 0; |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 368 | StringRef GroupName; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 369 | unsigned Flags = 0; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 370 | const MCExpr *Subsection = nullptr; |
David Majnemer | a4b521b | 2013-09-15 19:24:16 +0000 | [diff] [blame] | 371 | bool UseLastGroup = false; |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 372 | StringRef UniqueStr; |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 373 | int64_t UniqueID = ~0; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 374 | |
| 375 | // Set the defaults first. |
| 376 | if (SectionName == ".fini" || SectionName == ".init" || |
| 377 | SectionName == ".rodata") |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 378 | Flags |= ELF::SHF_ALLOC; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 379 | if (SectionName == ".fini" || SectionName == ".init") |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 380 | Flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 381 | |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 382 | if (getLexer().is(AsmToken::Comma)) { |
| 383 | Lex(); |
| 384 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 385 | if (IsPush && getLexer().isNot(AsmToken::String)) { |
| 386 | if (getParser().parseExpression(Subsection)) |
| 387 | return true; |
| 388 | if (getLexer().isNot(AsmToken::Comma)) |
| 389 | goto EndStmt; |
| 390 | Lex(); |
| 391 | } |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 392 | |
Venkatraman Govindaraju | bf70566 | 2014-03-01 06:21:00 +0000 | [diff] [blame] | 393 | unsigned extraFlags; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 394 | |
Venkatraman Govindaraju | bf70566 | 2014-03-01 06:21:00 +0000 | [diff] [blame] | 395 | if (getLexer().isNot(AsmToken::String)) { |
| 396 | if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax() |
| 397 | || getLexer().isNot(AsmToken::Hash)) |
| 398 | return TokError("expected string in directive"); |
| 399 | extraFlags = parseSunStyleSectionFlags(); |
| 400 | } else { |
| 401 | StringRef FlagsStr = getTok().getStringContents(); |
| 402 | Lex(); |
| 403 | extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup); |
| 404 | } |
| 405 | |
Benjamin Kramer | ac511ca | 2013-09-15 19:53:20 +0000 | [diff] [blame] | 406 | if (extraFlags == -1U) |
Rafael Espindola | f8e127e | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 407 | return TokError("unknown flag"); |
| 408 | Flags |= extraFlags; |
| 409 | |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 410 | bool Mergeable = Flags & ELF::SHF_MERGE; |
| 411 | bool Group = Flags & ELF::SHF_GROUP; |
David Majnemer | a4b521b | 2013-09-15 19:24:16 +0000 | [diff] [blame] | 412 | if (Group && UseLastGroup) |
| 413 | return TokError("Section cannot specifiy a group name while also acting " |
| 414 | "as a member of the last group"); |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 415 | |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 416 | if (getLexer().isNot(AsmToken::Comma)) { |
| 417 | if (Mergeable) |
| 418 | return TokError("Mergeable section must specify the type"); |
| 419 | if (Group) |
| 420 | return TokError("Group section must specify the type"); |
| 421 | } else { |
| 422 | Lex(); |
David Majnemer | f90c3b5 | 2013-09-21 05:25:12 +0000 | [diff] [blame] | 423 | if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) || |
| 424 | getLexer().is(AsmToken::String)) { |
| 425 | if (!getLexer().is(AsmToken::String)) |
| 426 | Lex(); |
| 427 | } else |
| 428 | return TokError("expected '@<type>', '%<type>' or \"<type>\""); |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 429 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 430 | if (getParser().parseIdentifier(TypeName)) |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 431 | return TokError("expected identifier in directive"); |
| 432 | |
| 433 | if (Mergeable) { |
| 434 | if (getLexer().isNot(AsmToken::Comma)) |
| 435 | return TokError("expected the entry size"); |
| 436 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 437 | if (getParser().parseAbsoluteExpression(Size)) |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 438 | return true; |
| 439 | if (Size <= 0) |
| 440 | return TokError("entry size must be positive"); |
| 441 | } |
| 442 | |
| 443 | if (Group) { |
| 444 | if (getLexer().isNot(AsmToken::Comma)) |
| 445 | return TokError("expected group name"); |
| 446 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 447 | if (getParser().parseIdentifier(GroupName)) |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 448 | return true; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 449 | if (getLexer().is(AsmToken::Comma)) { |
| 450 | Lex(); |
Rafael Espindola | 8aefb66 | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 451 | StringRef Linkage; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 452 | if (getParser().parseIdentifier(Linkage)) |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 453 | return true; |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 454 | if (Linkage != "comdat") |
| 455 | return TokError("Linkage must be 'comdat'"); |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 458 | if (getLexer().is(AsmToken::Comma)) { |
| 459 | Lex(); |
| 460 | if (getParser().parseIdentifier(UniqueStr)) |
| 461 | return TokError("expected identifier in directive"); |
| 462 | if (UniqueStr != "unique") |
| 463 | return TokError("expected 'unique'"); |
Rafael Espindola | 41e2b5c | 2015-04-06 16:34:41 +0000 | [diff] [blame] | 464 | if (getLexer().isNot(AsmToken::Comma)) |
| 465 | return TokError("expected commma"); |
| 466 | Lex(); |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 467 | if (getParser().parseAbsoluteExpression(UniqueID)) |
| 468 | return true; |
| 469 | if (UniqueID < 0) |
| 470 | return TokError("unique id must be positive"); |
| 471 | if (!isUInt<32>(UniqueID) || UniqueID == ~0U) |
| 472 | return TokError("unique id is too large"); |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 473 | } |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 477 | EndStmt: |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 478 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 479 | return TokError("unexpected token in directive"); |
| 480 | |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 481 | unsigned Type = ELF::SHT_PROGBITS; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 482 | |
Joerg Sonnenberger | e2bb314 | 2013-02-16 00:32:53 +0000 | [diff] [blame] | 483 | if (TypeName.empty()) { |
| 484 | if (SectionName.startswith(".note")) |
| 485 | Type = ELF::SHT_NOTE; |
| 486 | else if (SectionName == ".init_array") |
| 487 | Type = ELF::SHT_INIT_ARRAY; |
| 488 | else if (SectionName == ".fini_array") |
| 489 | Type = ELF::SHT_FINI_ARRAY; |
| 490 | else if (SectionName == ".preinit_array") |
| 491 | Type = ELF::SHT_PREINIT_ARRAY; |
| 492 | } else { |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 493 | if (TypeName == "init_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 494 | Type = ELF::SHT_INIT_ARRAY; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 495 | else if (TypeName == "fini_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 496 | Type = ELF::SHT_FINI_ARRAY; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 497 | else if (TypeName == "preinit_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 498 | Type = ELF::SHT_PREINIT_ARRAY; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 499 | else if (TypeName == "nobits") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 500 | Type = ELF::SHT_NOBITS; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 501 | else if (TypeName == "progbits") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 502 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | 9ae2d05 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 503 | else if (TypeName == "note") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 504 | Type = ELF::SHT_NOTE; |
Rafael Espindola | 4b7b7fb | 2011-01-23 05:43:40 +0000 | [diff] [blame] | 505 | else if (TypeName == "unwind") |
| 506 | Type = ELF::SHT_X86_64_UNWIND; |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 507 | else |
| 508 | return TokError("unknown section type"); |
| 509 | } |
| 510 | |
David Majnemer | a4b521b | 2013-09-15 19:24:16 +0000 | [diff] [blame] | 511 | if (UseLastGroup) { |
| 512 | MCSectionSubPair CurrentSection = getStreamer().getCurrentSection(); |
| 513 | if (const MCSectionELF *Section = |
| 514 | cast_or_null<MCSectionELF>(CurrentSection.first)) |
| 515 | if (const MCSymbol *Group = Section->getGroup()) { |
| 516 | GroupName = Group->getName(); |
| 517 | Flags |= ELF::SHF_GROUP; |
| 518 | } |
| 519 | } |
| 520 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 521 | MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags, |
| 522 | Size, GroupName, UniqueID); |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 523 | getStreamer().SwitchSection(ELFSection, Subsection); |
| 524 | |
| 525 | if (getContext().getGenDwarfForAssembly()) { |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 526 | bool InsertResult = getContext().addGenDwarfSection(ELFSection); |
| 527 | if (InsertResult) { |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 528 | if (getContext().getDwarfVersion() <= 2) |
Oliver Stannard | 14f97d0 | 2014-09-22 10:45:16 +0000 | [diff] [blame] | 529 | Warning(loc, "DWARF2 only supports one section per compilation unit"); |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 530 | |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 531 | if (!ELFSection->getBeginSymbol()) { |
| 532 | MCSymbol *SectionStartSymbol = getContext().createTempSymbol(); |
| 533 | getStreamer().EmitLabel(SectionStartSymbol); |
| 534 | ELFSection->setBeginSymbol(SectionStartSymbol); |
| 535 | } |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Eli Friedman | 9e36dd0 | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | |
Benjamin Kramer | e39017c | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 542 | bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 543 | MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 544 | if (PreviousSection.first == nullptr) |
Rafael Espindola | 58ac6e1 | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 545 | return TokError(".previous without corresponding .section"); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 546 | getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); |
Benjamin Kramer | e39017c | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 547 | |
| 548 | return false; |
| 549 | } |
| 550 | |
Saleem Abdulrasool | 13b414a | 2014-06-08 00:34:34 +0000 | [diff] [blame] | 551 | static MCSymbolAttr MCAttrForString(StringRef Type) { |
| 552 | return StringSwitch<MCSymbolAttr>(Type) |
| 553 | .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction) |
| 554 | .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject) |
| 555 | .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS) |
| 556 | .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon) |
| 557 | .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType) |
| 558 | .Cases("STT_GNU_IFUNC", "gnu_indirect_function", |
| 559 | MCSA_ELF_TypeIndFunction) |
| 560 | .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) |
| 561 | .Default(MCSA_Invalid); |
| 562 | } |
| 563 | |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 564 | /// ParseDirectiveELFType |
David Majnemer | f90c3b5 | 2013-09-21 05:25:12 +0000 | [diff] [blame] | 565 | /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE> |
| 566 | /// ::= .type identifier , #attribute |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 567 | /// ::= .type identifier , @attribute |
David Majnemer | f90c3b5 | 2013-09-21 05:25:12 +0000 | [diff] [blame] | 568 | /// ::= .type identifier , %attribute |
| 569 | /// ::= .type identifier , "attribute" |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 570 | bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { |
| 571 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 572 | if (getParser().parseIdentifier(Name)) |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 573 | return TokError("expected identifier in directive"); |
| 574 | |
| 575 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 576 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 577 | |
Saleem Abdulrasool | 13b414a | 2014-06-08 00:34:34 +0000 | [diff] [blame] | 578 | // NOTE the comma is optional in all cases. It is only documented as being |
| 579 | // optional in the first case, however, GAS will silently treat the comma as |
| 580 | // optional in all cases. Furthermore, although the documentation states that |
| 581 | // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS |
| 582 | // accepts both the upper case name as well as the lower case aliases. |
| 583 | if (getLexer().is(AsmToken::Comma)) |
| 584 | Lex(); |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 585 | |
Saleem Abdulrasool | 13b414a | 2014-06-08 00:34:34 +0000 | [diff] [blame] | 586 | if (getLexer().isNot(AsmToken::Identifier) && |
Gabor Ballabas | af06a88 | 2015-07-01 08:58:49 +0000 | [diff] [blame] | 587 | getLexer().isNot(AsmToken::Hash) && |
| 588 | getLexer().isNot(AsmToken::Percent) && |
| 589 | getLexer().isNot(AsmToken::String)) { |
| 590 | if (!getLexer().getAllowAtInIdentifier()) |
| 591 | return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', " |
| 592 | "'%<type>' or \"<type>\""); |
| 593 | else if (getLexer().isNot(AsmToken::At)) |
| 594 | return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " |
| 595 | "'%<type>' or \"<type>\""); |
| 596 | } |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 597 | |
Saleem Abdulrasool | 13b414a | 2014-06-08 00:34:34 +0000 | [diff] [blame] | 598 | if (getLexer().isNot(AsmToken::String) && |
| 599 | getLexer().isNot(AsmToken::Identifier)) |
| 600 | Lex(); |
| 601 | |
| 602 | SMLoc TypeLoc = getLexer().getLoc(); |
| 603 | |
| 604 | StringRef Type; |
| 605 | if (getParser().parseIdentifier(Type)) |
| 606 | return TokError("expected symbol type in directive"); |
| 607 | |
| 608 | MCSymbolAttr Attr = MCAttrForString(Type); |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 609 | if (Attr == MCSA_Invalid) |
| 610 | return Error(TypeLoc, "unsupported attribute in '.type' directive"); |
| 611 | |
| 612 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 613 | return TokError("unexpected token in '.type' directive"); |
Michael J. Spencer | 3d89823 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 614 | Lex(); |
| 615 | |
| 616 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
| 617 | |
| 618 | return false; |
| 619 | } |
| 620 | |
Rafael Espindola | c9fb35e | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 621 | /// ParseDirectiveIdent |
| 622 | /// ::= .ident string |
| 623 | bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { |
| 624 | if (getLexer().isNot(AsmToken::String)) |
| 625 | return TokError("unexpected token in '.ident' directive"); |
| 626 | |
| 627 | StringRef Data = getTok().getIdentifier(); |
| 628 | |
| 629 | Lex(); |
| 630 | |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 631 | getStreamer().EmitIdent(Data); |
Rafael Espindola | c9fb35e | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 635 | /// ParseDirectiveSymver |
| 636 | /// ::= .symver foo, bar2@zed |
| 637 | bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { |
| 638 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 639 | if (getParser().parseIdentifier(Name)) |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 640 | return TokError("expected identifier in directive"); |
| 641 | |
| 642 | if (getLexer().isNot(AsmToken::Comma)) |
| 643 | return TokError("expected a comma"); |
| 644 | |
David Peixotto | c0f92a2 | 2014-01-15 22:40:02 +0000 | [diff] [blame] | 645 | // ARM assembly uses @ for a comment... |
| 646 | // except when parsing the second parameter of the .symver directive. |
| 647 | // Force the next symbol to allow @ in the identifier, which is |
| 648 | // required for this directive and then reset it to its initial state. |
| 649 | const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier(); |
| 650 | getLexer().setAllowAtInIdentifier(true); |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 651 | Lex(); |
David Peixotto | c0f92a2 | 2014-01-15 22:40:02 +0000 | [diff] [blame] | 652 | getLexer().setAllowAtInIdentifier(AllowAtInIdentifier); |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 653 | |
| 654 | StringRef AliasName; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 655 | if (getParser().parseIdentifier(AliasName)) |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 656 | return TokError("expected identifier in directive"); |
| 657 | |
| 658 | if (AliasName.find('@') == StringRef::npos) |
| 659 | return TokError("expected a '@' in the name"); |
| 660 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 661 | MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName); |
| 662 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 663 | const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext()); |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 664 | |
| 665 | getStreamer().EmitAssignment(Alias, Value); |
| 666 | return false; |
| 667 | } |
| 668 | |
Benjamin Kramer | 6bee7f7 | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 669 | /// ParseDirectiveVersion |
| 670 | /// ::= .version string |
| 671 | bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { |
| 672 | if (getLexer().isNot(AsmToken::String)) |
| 673 | return TokError("unexpected token in '.version' directive"); |
| 674 | |
| 675 | StringRef Data = getTok().getIdentifier(); |
| 676 | |
| 677 | Lex(); |
| 678 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 679 | MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0); |
Benjamin Kramer | 6bee7f7 | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 680 | |
| 681 | getStreamer().PushSection(); |
| 682 | getStreamer().SwitchSection(Note); |
| 683 | getStreamer().EmitIntValue(Data.size()+1, 4); // namesz. |
| 684 | getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description). |
| 685 | getStreamer().EmitIntValue(1, 4); // type = NT_VERSION. |
Eric Christopher | e3ab3d0 | 2013-01-09 01:57:54 +0000 | [diff] [blame] | 686 | getStreamer().EmitBytes(Data); // name. |
Benjamin Kramer | 6bee7f7 | 2012-05-12 14:30:47 +0000 | [diff] [blame] | 687 | getStreamer().EmitIntValue(0, 1); // terminate the string. |
| 688 | getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment. |
| 689 | getStreamer().PopSection(); |
| 690 | return false; |
| 691 | } |
| 692 | |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 693 | /// ParseDirectiveWeakref |
| 694 | /// ::= .weakref foo, bar |
| 695 | bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { |
| 696 | // FIXME: Share code with the other alias building directives. |
| 697 | |
| 698 | StringRef AliasName; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 699 | if (getParser().parseIdentifier(AliasName)) |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 700 | return TokError("expected identifier in directive"); |
| 701 | |
| 702 | if (getLexer().isNot(AsmToken::Comma)) |
| 703 | return TokError("expected a comma"); |
| 704 | |
| 705 | Lex(); |
| 706 | |
| 707 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 708 | if (getParser().parseIdentifier(Name)) |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 709 | return TokError("expected identifier in directive"); |
| 710 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 711 | MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName); |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 712 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 713 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 714 | |
| 715 | getStreamer().EmitWeakReference(Alias, Sym); |
| 716 | return false; |
| 717 | } |
| 718 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 719 | bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 720 | const MCExpr *Subsection = nullptr; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 721 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 722 | if (getParser().parseExpression(Subsection)) |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 727 | return TokError("unexpected token in directive"); |
| 728 | |
| 729 | getStreamer().SubSection(Subsection); |
| 730 | return false; |
| 731 | } |
| 732 | |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 733 | namespace llvm { |
| 734 | |
| 735 | MCAsmParserExtension *createELFAsmParser() { |
| 736 | return new ELFAsmParser; |
| 737 | } |
| 738 | |
| 739 | } |