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" |
Eli Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSectionELF.h" |
| 16 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | f21e4e9 | 2010-07-18 18:31:42 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class ELFAsmParser : public MCAsmParserExtension { |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 23 | template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)> |
| 24 | void AddDirectiveHandler(StringRef Directive) { |
| 25 | getParser().AddDirectiveHandler(this, Directive, |
| 26 | HandleDirective<ELFAsmParser, Handler>); |
| 27 | } |
| 28 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 29 | bool ParseSectionSwitch(StringRef Section, unsigned Type, |
| 30 | unsigned Flags, SectionKind Kind); |
| 31 | |
| 32 | public: |
| 33 | ELFAsmParser() {} |
| 34 | |
| 35 | virtual void Initialize(MCAsmParser &Parser) { |
| 36 | // Call the base implementation. |
| 37 | this->MCAsmParserExtension::Initialize(Parser); |
| 38 | |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 39 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data"); |
| 40 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text"); |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 41 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss"); |
| 42 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata"); |
| 43 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata"); |
| 44 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss"); |
| 45 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel"); |
| 46 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); |
| 47 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local"); |
| 48 | AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 49 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); |
| 50 | AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size"); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 51 | AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous"); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 54 | // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is |
| 55 | // the best way for us to get access to it? |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 56 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
Rafael Espindola | 0453dd9 | 2010-09-27 21:40:27 +0000 | [diff] [blame^] | 57 | return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS, |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 58 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 59 | SectionKind::getDataRel()); |
| 60 | } |
| 61 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
Rafael Espindola | 0453dd9 | 2010-09-27 21:40:27 +0000 | [diff] [blame^] | 62 | return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS, |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 63 | MCSectionELF::SHF_EXECINSTR | |
| 64 | MCSectionELF::SHF_ALLOC, SectionKind::getText()); |
| 65 | } |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 66 | bool ParseSectionDirectiveBSS(StringRef, SMLoc) { |
Rafael Espindola | 0453dd9 | 2010-09-27 21:40:27 +0000 | [diff] [blame^] | 67 | return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS, |
Matt Fleming | f525c2a | 2010-07-20 21:12:46 +0000 | [diff] [blame] | 68 | MCSectionELF::SHF_WRITE | |
| 69 | MCSectionELF::SHF_ALLOC, SectionKind::getBSS()); |
| 70 | } |
| 71 | bool ParseSectionDirectiveRoData(StringRef, SMLoc) { |
| 72 | return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS, |
| 73 | MCSectionELF::SHF_ALLOC, |
| 74 | SectionKind::getReadOnly()); |
| 75 | } |
| 76 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
| 77 | return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS, |
| 78 | MCSectionELF::SHF_ALLOC | |
| 79 | MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE, |
| 80 | SectionKind::getThreadData()); |
| 81 | } |
| 82 | bool ParseSectionDirectiveTBSS(StringRef, SMLoc) { |
| 83 | return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS, |
| 84 | MCSectionELF::SHF_ALLOC | |
| 85 | MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE, |
| 86 | SectionKind::getThreadBSS()); |
| 87 | } |
| 88 | bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { |
| 89 | return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS, |
| 90 | MCSectionELF::SHF_ALLOC | |
| 91 | MCSectionELF::SHF_WRITE, |
| 92 | SectionKind::getDataRel()); |
| 93 | } |
| 94 | bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { |
| 95 | return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS, |
| 96 | MCSectionELF::SHF_ALLOC | |
| 97 | MCSectionELF::SHF_WRITE, |
| 98 | SectionKind::getReadOnlyWithRel()); |
| 99 | } |
| 100 | bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) { |
| 101 | return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS, |
| 102 | MCSectionELF::SHF_ALLOC | |
| 103 | MCSectionELF::SHF_WRITE, |
| 104 | SectionKind::getReadOnlyWithRelLocal()); |
| 105 | } |
| 106 | bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { |
| 107 | return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS, |
| 108 | MCSectionELF::SHF_ALLOC | |
| 109 | MCSectionELF::SHF_WRITE, |
| 110 | SectionKind::getDataRel()); |
| 111 | } |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 112 | bool ParseDirectiveSection(StringRef, SMLoc); |
| 113 | bool ParseDirectiveSize(StringRef, SMLoc); |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 114 | bool ParseDirectivePrevious(StringRef, SMLoc); |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | bool ParseSectionName(StringRef &SectionName); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, |
| 123 | unsigned Flags, SectionKind Kind) { |
| 124 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 125 | return TokError("unexpected token in section switching directive"); |
| 126 | Lex(); |
| 127 | |
| 128 | getStreamer().SwitchSection(getContext().getELFSection( |
| 129 | Section, Type, Flags, Kind)); |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 134 | bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 135 | StringRef Name; |
| 136 | if (getParser().ParseIdentifier(Name)) |
| 137 | return TokError("expected identifier in directive"); |
| 138 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);; |
| 139 | |
| 140 | if (getLexer().isNot(AsmToken::Comma)) |
| 141 | return TokError("unexpected token in directive"); |
| 142 | Lex(); |
| 143 | |
| 144 | const MCExpr *Expr; |
| 145 | if (getParser().ParseExpression(Expr)) |
| 146 | return true; |
| 147 | |
| 148 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 149 | return TokError("unexpected token in directive"); |
| 150 | |
| 151 | getStreamer().EmitELFSize(Sym, Expr); |
| 152 | return false; |
| 153 | } |
| 154 | |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 155 | bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { |
| 156 | // A section name can contain -, so we cannot just use |
| 157 | // ParseIdentifier. |
| 158 | SMLoc FirstLoc = getLexer().getLoc(); |
| 159 | unsigned Size = 0; |
| 160 | |
| 161 | for (;;) { |
| 162 | StringRef Tmp; |
| 163 | unsigned CurSize; |
| 164 | |
| 165 | SMLoc PrevLoc = getLexer().getLoc(); |
| 166 | if (getLexer().is(AsmToken::Minus)) { |
| 167 | CurSize = 1; |
| 168 | Lex(); // Consume the "-". |
| 169 | } else if (!getParser().ParseIdentifier(Tmp)) |
| 170 | CurSize = Tmp.size(); |
| 171 | else |
| 172 | break; |
| 173 | |
| 174 | Size += CurSize; |
| 175 | SectionName = StringRef(FirstLoc.getPointer(), Size); |
| 176 | |
| 177 | // Make sure the following token is adjacent. |
| 178 | if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) |
| 179 | break; |
| 180 | } |
| 181 | if (Size == 0) |
| 182 | return true; |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 187 | // FIXME: This is a work in progress. |
| 188 | bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { |
| 189 | StringRef SectionName; |
Rafael Espindola | 34e3d0c | 2010-09-16 17:05:55 +0000 | [diff] [blame] | 190 | |
| 191 | if (ParseSectionName(SectionName)) |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 192 | return TokError("expected identifier in directive"); |
| 193 | |
| 194 | std::string FlagsStr; |
| 195 | StringRef TypeName; |
| 196 | int64_t Size = 0; |
| 197 | if (getLexer().is(AsmToken::Comma)) { |
| 198 | Lex(); |
| 199 | |
| 200 | if (getLexer().isNot(AsmToken::String)) |
| 201 | return TokError("expected string in directive"); |
| 202 | |
| 203 | FlagsStr = getTok().getStringContents(); |
| 204 | Lex(); |
| 205 | |
| 206 | AsmToken::TokenKind TypeStartToken; |
| 207 | if (getContext().getAsmInfo().getCommentString()[0] == '@') |
| 208 | TypeStartToken = AsmToken::Percent; |
| 209 | else |
| 210 | TypeStartToken = AsmToken::At; |
| 211 | |
| 212 | if (getLexer().is(AsmToken::Comma)) { |
| 213 | Lex(); |
| 214 | if (getLexer().is(TypeStartToken)) { |
| 215 | Lex(); |
| 216 | if (getParser().ParseIdentifier(TypeName)) |
| 217 | return TokError("expected identifier in directive"); |
| 218 | |
| 219 | if (getLexer().is(AsmToken::Comma)) { |
| 220 | Lex(); |
| 221 | |
| 222 | if (getParser().ParseAbsoluteExpression(Size)) |
| 223 | return true; |
| 224 | |
| 225 | if (Size <= 0) |
| 226 | return TokError("section size must be positive"); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 233 | return TokError("unexpected token in directive"); |
| 234 | |
| 235 | unsigned Flags = 0; |
| 236 | for (unsigned i = 0; i < FlagsStr.size(); i++) { |
| 237 | switch (FlagsStr[i]) { |
| 238 | case 'a': |
| 239 | Flags |= MCSectionELF::SHF_ALLOC; |
| 240 | break; |
| 241 | case 'x': |
| 242 | Flags |= MCSectionELF::SHF_EXECINSTR; |
| 243 | break; |
| 244 | case 'w': |
| 245 | Flags |= MCSectionELF::SHF_WRITE; |
| 246 | break; |
| 247 | case 'M': |
| 248 | Flags |= MCSectionELF::SHF_MERGE; |
| 249 | break; |
| 250 | case 'S': |
| 251 | Flags |= MCSectionELF::SHF_STRINGS; |
| 252 | break; |
| 253 | case 'T': |
| 254 | Flags |= MCSectionELF::SHF_TLS; |
| 255 | break; |
| 256 | case 'c': |
| 257 | Flags |= MCSectionELF::XCORE_SHF_CP_SECTION; |
| 258 | break; |
| 259 | case 'd': |
| 260 | Flags |= MCSectionELF::XCORE_SHF_DP_SECTION; |
| 261 | break; |
| 262 | default: |
| 263 | return TokError("unknown flag"); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | unsigned Type = MCSectionELF::SHT_NULL; |
| 268 | if (!TypeName.empty()) { |
| 269 | if (TypeName == "init_array") |
| 270 | Type = MCSectionELF::SHT_INIT_ARRAY; |
| 271 | else if (TypeName == "fini_array") |
| 272 | Type = MCSectionELF::SHT_FINI_ARRAY; |
| 273 | else if (TypeName == "preinit_array") |
| 274 | Type = MCSectionELF::SHT_PREINIT_ARRAY; |
| 275 | else if (TypeName == "nobits") |
| 276 | Type = MCSectionELF::SHT_NOBITS; |
| 277 | else if (TypeName == "progbits") |
| 278 | Type = MCSectionELF::SHT_PROGBITS; |
| 279 | else |
| 280 | return TokError("unknown section type"); |
| 281 | } |
| 282 | |
| 283 | SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR) |
| 284 | ? SectionKind::getText() |
| 285 | : SectionKind::getDataRel(); |
| 286 | getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type, |
| 287 | Flags, Kind, false)); |
| 288 | return false; |
| 289 | } |
| 290 | |
Benjamin Kramer | 1674b0b | 2010-09-02 18:53:37 +0000 | [diff] [blame] | 291 | bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { |
| 292 | const MCSection *PreviousSection = getStreamer().getPreviousSection(); |
| 293 | if (PreviousSection != NULL) |
| 294 | getStreamer().SwitchSection(PreviousSection); |
| 295 | |
| 296 | return false; |
| 297 | } |
| 298 | |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 299 | namespace llvm { |
| 300 | |
| 301 | MCAsmParserExtension *createELFAsmParser() { |
| 302 | return new ELFAsmParser; |
| 303 | } |
| 304 | |
| 305 | } |