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