Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 1 | //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringSwitch.h" |
Eli Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionELF.h" |
| 18 | #include "llvm/MC/MCStreamer.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ELF.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class ELFAsmParser : public MCAsmParserExtension { |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 25 | template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)> |
| 26 | void AddDirectiveHandler(StringRef Directive) { |
| 27 | getParser().AddDirectiveHandler(this, Directive, |
| 28 | HandleDirective<ELFAsmParser, Handler>); |
| 29 | } |
| 30 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 31 | bool ParseSectionSwitch(StringRef Section, unsigned Type, |
| 32 | unsigned Flags, SectionKind Kind); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 33 | bool SeenIdent; |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 34 | |
| 35 | public: |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 36 | ELFAsmParser() : SeenIdent(false) { |
| 37 | BracketExpressionsSupported = true; |
| 38 | } |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 39 | |
| 40 | virtual void Initialize(MCAsmParser &Parser) { |
| 41 | // Call the base implementation. |
| 42 | this->MCAsmParserExtension::Initialize(Parser); |
| 43 | |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 44 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data"); |
| 45 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text"); |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 46 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss"); |
| 47 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata"); |
| 48 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata"); |
| 49 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss"); |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 50 | AddDirectiveHandler< |
| 51 | &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel"); |
| 52 | AddDirectiveHandler< |
| 53 | &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); |
| 54 | AddDirectiveHandler< |
| 55 | &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local"); |
| 56 | AddDirectiveHandler< |
| 57 | &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 58 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); |
Jim Grosbach | cc866d5 | 2011-07-25 17:11:29 +0000 | [diff] [blame] | 59 | AddDirectiveHandler< |
| 60 | &ELFAsmParser::ParseDirectivePushSection>(".pushsection"); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 61 | AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection"); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 62 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size"); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 63 | AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous"); |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 64 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type"); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 65 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident"); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 66 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver"); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 67 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref"); |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 68 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak"); |
| 69 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local"); |
| 70 | AddDirectiveHandler< |
| 71 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected"); |
| 72 | AddDirectiveHandler< |
| 73 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal"); |
| 74 | AddDirectiveHandler< |
| 75 | &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden"); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Rafael Espindola | d80781b | 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 | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 80 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 81 | return ParseSectionSwitch(".data", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 82 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 83 | SectionKind::getDataRel()); |
| 84 | } |
| 85 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 86 | return ParseSectionSwitch(".text", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 87 | ELF::SHF_EXECINSTR | |
| 88 | ELF::SHF_ALLOC, SectionKind::getText()); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 89 | } |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 90 | bool ParseSectionDirectiveBSS(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 91 | return ParseSectionSwitch(".bss", ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 92 | ELF::SHF_WRITE | |
| 93 | ELF::SHF_ALLOC, SectionKind::getBSS()); |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 94 | } |
| 95 | bool ParseSectionDirectiveRoData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 96 | return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 97 | ELF::SHF_ALLOC, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 98 | SectionKind::getReadOnly()); |
| 99 | } |
| 100 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 101 | return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 102 | ELF::SHF_ALLOC | |
| 103 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 104 | SectionKind::getThreadData()); |
| 105 | } |
| 106 | bool ParseSectionDirectiveTBSS(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 107 | return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 108 | ELF::SHF_ALLOC | |
| 109 | ELF::SHF_TLS | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 110 | SectionKind::getThreadBSS()); |
| 111 | } |
| 112 | bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 113 | return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 114 | ELF::SHF_ALLOC | |
| 115 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 116 | SectionKind::getDataRel()); |
| 117 | } |
| 118 | bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 119 | return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 120 | ELF::SHF_ALLOC | |
| 121 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 122 | SectionKind::getReadOnlyWithRel()); |
| 123 | } |
| 124 | bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 125 | return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 126 | ELF::SHF_ALLOC | |
| 127 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 128 | SectionKind::getReadOnlyWithRelLocal()); |
| 129 | } |
| 130 | bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 131 | return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 132 | ELF::SHF_ALLOC | |
| 133 | ELF::SHF_WRITE, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 134 | SectionKind::getDataRel()); |
| 135 | } |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 136 | bool ParseDirectivePushSection(StringRef, SMLoc); |
| 137 | bool ParseDirectivePopSection(StringRef, SMLoc); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 138 | bool ParseDirectiveSection(StringRef, SMLoc); |
| 139 | bool ParseDirectiveSize(StringRef, SMLoc); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 140 | bool ParseDirectivePrevious(StringRef, SMLoc); |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 141 | bool ParseDirectiveType(StringRef, SMLoc); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 142 | bool ParseDirectiveIdent(StringRef, SMLoc); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 143 | bool ParseDirectiveSymver(StringRef, SMLoc); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 144 | bool ParseDirectiveWeakref(StringRef, SMLoc); |
Jim Grosbach | f2a35fb | 2011-07-25 17:55:35 +0000 | [diff] [blame] | 145 | bool ParseDirectiveSymbolAttribute(StringRef, SMLoc); |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | bool ParseSectionName(StringRef &SectionName); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } |
| 152 | |
Jim Grosbach | f2a35fb | 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 | |
| 168 | if (getParser().ParseIdentifier(Name)) |
| 169 | return TokError("expected identifier in directive"); |
| 170 | |
| 171 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 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 | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 188 | bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, |
| 189 | unsigned Flags, SectionKind Kind) { |
| 190 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 191 | return TokError("unexpected token in section switching directive"); |
| 192 | Lex(); |
| 193 | |
| 194 | getStreamer().SwitchSection(getContext().getELFSection( |
| 195 | Section, Type, Flags, Kind)); |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 200 | bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 201 | StringRef Name; |
| 202 | if (getParser().ParseIdentifier(Name)) |
| 203 | return TokError("expected identifier in directive"); |
| 204 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);; |
| 205 | |
| 206 | if (getLexer().isNot(AsmToken::Comma)) |
| 207 | return TokError("unexpected token in directive"); |
| 208 | Lex(); |
| 209 | |
| 210 | const MCExpr *Expr; |
| 211 | if (getParser().ParseExpression(Expr)) |
| 212 | return true; |
| 213 | |
| 214 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 215 | return TokError("unexpected token in directive"); |
| 216 | |
| 217 | getStreamer().EmitELFSize(Sym, Expr); |
| 218 | return false; |
| 219 | } |
| 220 | |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 221 | bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { |
| 222 | // A section name can contain -, so we cannot just use |
| 223 | // ParseIdentifier. |
| 224 | SMLoc FirstLoc = getLexer().getLoc(); |
| 225 | unsigned Size = 0; |
| 226 | |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 227 | if (getLexer().is(AsmToken::String)) { |
| 228 | SectionName = getTok().getIdentifier(); |
| 229 | Lex(); |
| 230 | return false; |
| 231 | } |
| 232 | |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 233 | for (;;) { |
| 234 | StringRef Tmp; |
| 235 | unsigned CurSize; |
| 236 | |
| 237 | SMLoc PrevLoc = getLexer().getLoc(); |
| 238 | if (getLexer().is(AsmToken::Minus)) { |
| 239 | CurSize = 1; |
| 240 | Lex(); // Consume the "-". |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 241 | } else if (getLexer().is(AsmToken::String)) { |
| 242 | CurSize = getTok().getIdentifier().size() + 2; |
| 243 | Lex(); |
| 244 | } else if (getLexer().is(AsmToken::Identifier)) { |
| 245 | CurSize = getTok().getIdentifier().size(); |
| 246 | Lex(); |
| 247 | } else { |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 248 | break; |
Rafael Espindola | 184640e | 2011-01-24 18:02:54 +0000 | [diff] [blame] | 249 | } |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 250 | |
| 251 | Size += CurSize; |
| 252 | SectionName = StringRef(FirstLoc.getPointer(), Size); |
| 253 | |
| 254 | // Make sure the following token is adjacent. |
| 255 | if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) |
| 256 | break; |
| 257 | } |
| 258 | if (Size == 0) |
| 259 | return true; |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 264 | static SectionKind computeSectionKind(unsigned Flags) { |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 265 | if (Flags & ELF::SHF_EXECINSTR) |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 266 | return SectionKind::getText(); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 267 | if (Flags & ELF::SHF_TLS) |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 268 | return SectionKind::getThreadData(); |
| 269 | return SectionKind::getDataRel(); |
| 270 | } |
| 271 | |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 272 | static int parseSectionFlags(StringRef flagsStr) { |
| 273 | int flags = 0; |
| 274 | |
| 275 | for (unsigned i = 0; i < flagsStr.size(); i++) { |
| 276 | switch (flagsStr[i]) { |
| 277 | case 'a': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 278 | flags |= ELF::SHF_ALLOC; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 279 | break; |
| 280 | case 'x': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 281 | flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 282 | break; |
| 283 | case 'w': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 284 | flags |= ELF::SHF_WRITE; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 285 | break; |
| 286 | case 'M': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 287 | flags |= ELF::SHF_MERGE; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 288 | break; |
| 289 | case 'S': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 290 | flags |= ELF::SHF_STRINGS; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 291 | break; |
| 292 | case 'T': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 293 | flags |= ELF::SHF_TLS; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 294 | break; |
| 295 | case 'c': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 296 | flags |= ELF::XCORE_SHF_CP_SECTION; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 297 | break; |
| 298 | case 'd': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 299 | flags |= ELF::XCORE_SHF_DP_SECTION; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 300 | break; |
| 301 | case 'G': |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 302 | flags |= ELF::SHF_GROUP; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 303 | break; |
| 304 | default: |
| 305 | return -1; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return flags; |
| 310 | } |
| 311 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 312 | bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) { |
| 313 | getStreamer().PushSection(); |
| 314 | |
| 315 | if (ParseDirectiveSection(s, loc)) { |
| 316 | getStreamer().PopSection(); |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { |
| 324 | if (!getStreamer().PopSection()) |
| 325 | return TokError(".popsection without corresponding .pushsection"); |
| 326 | return false; |
| 327 | } |
| 328 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 329 | // FIXME: This is a work in progress. |
| 330 | bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { |
| 331 | StringRef SectionName; |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 332 | |
| 333 | if (ParseSectionName(SectionName)) |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 334 | return TokError("expected identifier in directive"); |
| 335 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 336 | StringRef TypeName; |
| 337 | int64_t Size = 0; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 338 | StringRef GroupName; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 339 | unsigned Flags = 0; |
| 340 | |
| 341 | // Set the defaults first. |
| 342 | if (SectionName == ".fini" || SectionName == ".init" || |
| 343 | SectionName == ".rodata") |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 344 | Flags |= ELF::SHF_ALLOC; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 345 | if (SectionName == ".fini" || SectionName == ".init") |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 346 | Flags |= ELF::SHF_EXECINSTR; |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 347 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 348 | if (getLexer().is(AsmToken::Comma)) { |
| 349 | Lex(); |
| 350 | |
| 351 | if (getLexer().isNot(AsmToken::String)) |
| 352 | return TokError("expected string in directive"); |
| 353 | |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 354 | StringRef FlagsStr = getTok().getStringContents(); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 355 | Lex(); |
| 356 | |
Rafael Espindola | 6b8e435 | 2010-11-25 15:32:56 +0000 | [diff] [blame] | 357 | int extraFlags = parseSectionFlags(FlagsStr); |
| 358 | if (extraFlags < 0) |
| 359 | return TokError("unknown flag"); |
| 360 | Flags |= extraFlags; |
| 361 | |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 362 | bool Mergeable = Flags & ELF::SHF_MERGE; |
| 363 | bool Group = Flags & ELF::SHF_GROUP; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 364 | |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 365 | if (getLexer().isNot(AsmToken::Comma)) { |
| 366 | if (Mergeable) |
| 367 | return TokError("Mergeable section must specify the type"); |
| 368 | if (Group) |
| 369 | return TokError("Group section must specify the type"); |
| 370 | } else { |
| 371 | Lex(); |
Rafael Espindola | d4a3526 | 2010-11-12 15:47:08 +0000 | [diff] [blame] | 372 | if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At)) |
| 373 | return TokError("expected '@' or '%' before type"); |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 374 | |
| 375 | Lex(); |
| 376 | if (getParser().ParseIdentifier(TypeName)) |
| 377 | return TokError("expected identifier in directive"); |
| 378 | |
| 379 | if (Mergeable) { |
| 380 | if (getLexer().isNot(AsmToken::Comma)) |
| 381 | return TokError("expected the entry size"); |
| 382 | Lex(); |
| 383 | if (getParser().ParseAbsoluteExpression(Size)) |
| 384 | return true; |
| 385 | if (Size <= 0) |
| 386 | return TokError("entry size must be positive"); |
| 387 | } |
| 388 | |
| 389 | if (Group) { |
| 390 | if (getLexer().isNot(AsmToken::Comma)) |
| 391 | return TokError("expected group name"); |
| 392 | Lex(); |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 393 | if (getParser().ParseIdentifier(GroupName)) |
| 394 | return true; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 395 | if (getLexer().is(AsmToken::Comma)) { |
| 396 | Lex(); |
Rafael Espindola | f4b0f3e | 2010-10-28 21:33:33 +0000 | [diff] [blame] | 397 | StringRef Linkage; |
| 398 | if (getParser().ParseIdentifier(Linkage)) |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 399 | return true; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 400 | if (Linkage != "comdat") |
| 401 | return TokError("Linkage must be 'comdat'"); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 408 | return TokError("unexpected token in directive"); |
| 409 | |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 410 | unsigned Type = ELF::SHT_PROGBITS; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 411 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 412 | if (!TypeName.empty()) { |
| 413 | if (TypeName == "init_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 414 | Type = ELF::SHT_INIT_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 415 | else if (TypeName == "fini_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 416 | Type = ELF::SHT_FINI_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 417 | else if (TypeName == "preinit_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 418 | Type = ELF::SHT_PREINIT_ARRAY; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 419 | else if (TypeName == "nobits") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 420 | Type = ELF::SHT_NOBITS; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 421 | else if (TypeName == "progbits") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 422 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | 9897661 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 423 | else if (TypeName == "note") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 424 | Type = ELF::SHT_NOTE; |
Rafael Espindola | 0cf5e3d | 2011-01-23 05:43:40 +0000 | [diff] [blame] | 425 | else if (TypeName == "unwind") |
| 426 | Type = ELF::SHT_X86_64_UNWIND; |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 427 | else |
| 428 | return TokError("unknown section type"); |
| 429 | } |
| 430 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 431 | SectionKind Kind = computeSectionKind(Flags); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 432 | getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type, |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 433 | Flags, Kind, Size, |
| 434 | GroupName)); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 435 | return false; |
| 436 | } |
| 437 | |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 438 | bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { |
| 439 | const MCSection *PreviousSection = getStreamer().getPreviousSection(); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 440 | if (PreviousSection == NULL) |
| 441 | return TokError(".previous without corresponding .section"); |
| 442 | getStreamer().SwitchSection(PreviousSection); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 443 | |
| 444 | return false; |
| 445 | } |
| 446 | |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 447 | /// ParseDirectiveELFType |
| 448 | /// ::= .type identifier , @attribute |
| 449 | bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { |
| 450 | StringRef Name; |
| 451 | if (getParser().ParseIdentifier(Name)) |
| 452 | return TokError("expected identifier in directive"); |
| 453 | |
| 454 | // Handle the identifier as the key symbol. |
| 455 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 456 | |
| 457 | if (getLexer().isNot(AsmToken::Comma)) |
| 458 | return TokError("unexpected token in '.type' directive"); |
| 459 | Lex(); |
| 460 | |
Rafael Espindola | d4a3526 | 2010-11-12 15:47:08 +0000 | [diff] [blame] | 461 | if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At)) |
| 462 | return TokError("expected '@' or '%' before type"); |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 463 | Lex(); |
| 464 | |
| 465 | StringRef Type; |
| 466 | SMLoc TypeLoc; |
| 467 | |
| 468 | TypeLoc = getLexer().getLoc(); |
| 469 | if (getParser().ParseIdentifier(Type)) |
| 470 | return TokError("expected symbol type in directive"); |
| 471 | |
| 472 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type) |
| 473 | .Case("function", MCSA_ELF_TypeFunction) |
| 474 | .Case("object", MCSA_ELF_TypeObject) |
| 475 | .Case("tls_object", MCSA_ELF_TypeTLS) |
| 476 | .Case("common", MCSA_ELF_TypeCommon) |
| 477 | .Case("notype", MCSA_ELF_TypeNoType) |
Rafael Espindola | e13a0ff | 2010-11-13 04:51:02 +0000 | [diff] [blame] | 478 | .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) |
Roman Divacky | a0c17a4 | 2011-12-12 17:34:04 +0000 | [diff] [blame^] | 479 | .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction) |
Michael J. Spencer | e90ea13 | 2010-10-09 03:47:55 +0000 | [diff] [blame] | 480 | .Default(MCSA_Invalid); |
| 481 | |
| 482 | if (Attr == MCSA_Invalid) |
| 483 | return Error(TypeLoc, "unsupported attribute in '.type' directive"); |
| 484 | |
| 485 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 486 | return TokError("unexpected token in '.type' directive"); |
| 487 | |
| 488 | Lex(); |
| 489 | |
| 490 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
| 491 | |
| 492 | return false; |
| 493 | } |
| 494 | |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 495 | /// ParseDirectiveIdent |
| 496 | /// ::= .ident string |
| 497 | bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { |
| 498 | if (getLexer().isNot(AsmToken::String)) |
| 499 | return TokError("unexpected token in '.ident' directive"); |
| 500 | |
| 501 | StringRef Data = getTok().getIdentifier(); |
| 502 | |
| 503 | Lex(); |
| 504 | |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 505 | const MCSection *Comment = |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 506 | getContext().getELFSection(".comment", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 507 | ELF::SHF_MERGE | |
| 508 | ELF::SHF_STRINGS, |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 509 | SectionKind::getReadOnly(), |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 510 | 1, ""); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 511 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 512 | getStreamer().PushSection(); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 513 | getStreamer().SwitchSection(Comment); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 514 | if (!SeenIdent) { |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 515 | getStreamer().EmitIntValue(0, 1); |
Joerg Sonnenberger | a04663e | 2011-02-22 16:53:11 +0000 | [diff] [blame] | 516 | SeenIdent = true; |
| 517 | } |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 518 | getStreamer().EmitBytes(Data, 0); |
| 519 | getStreamer().EmitIntValue(0, 1); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 520 | getStreamer().PopSection(); |
Rafael Espindola | 61e3b91 | 2010-10-26 19:35:47 +0000 | [diff] [blame] | 521 | return false; |
| 522 | } |
| 523 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 524 | /// ParseDirectiveSymver |
| 525 | /// ::= .symver foo, bar2@zed |
| 526 | bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { |
| 527 | StringRef Name; |
| 528 | if (getParser().ParseIdentifier(Name)) |
| 529 | return TokError("expected identifier in directive"); |
| 530 | |
| 531 | if (getLexer().isNot(AsmToken::Comma)) |
| 532 | return TokError("expected a comma"); |
| 533 | |
| 534 | Lex(); |
| 535 | |
| 536 | StringRef AliasName; |
| 537 | if (getParser().ParseIdentifier(AliasName)) |
| 538 | return TokError("expected identifier in directive"); |
| 539 | |
| 540 | if (AliasName.find('@') == StringRef::npos) |
| 541 | return TokError("expected a '@' in the name"); |
| 542 | |
| 543 | MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); |
| 544 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 545 | const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext()); |
| 546 | |
| 547 | getStreamer().EmitAssignment(Alias, Value); |
| 548 | return false; |
| 549 | } |
| 550 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 551 | /// ParseDirectiveWeakref |
| 552 | /// ::= .weakref foo, bar |
| 553 | bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { |
| 554 | // FIXME: Share code with the other alias building directives. |
| 555 | |
| 556 | StringRef AliasName; |
| 557 | if (getParser().ParseIdentifier(AliasName)) |
| 558 | return TokError("expected identifier in directive"); |
| 559 | |
| 560 | if (getLexer().isNot(AsmToken::Comma)) |
| 561 | return TokError("expected a comma"); |
| 562 | |
| 563 | Lex(); |
| 564 | |
| 565 | StringRef Name; |
| 566 | if (getParser().ParseIdentifier(Name)) |
| 567 | return TokError("expected identifier in directive"); |
| 568 | |
| 569 | MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); |
| 570 | |
| 571 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 572 | |
| 573 | getStreamer().EmitWeakReference(Alias, Sym); |
| 574 | return false; |
| 575 | } |
| 576 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 577 | namespace llvm { |
| 578 | |
| 579 | MCAsmParserExtension *createELFAsmParser() { |
| 580 | return new ELFAsmParser; |
| 581 | } |
| 582 | |
| 583 | } |