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