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