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