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