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