2010-07-13 05:23:32 +08:00
|
|
|
//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
|
2010-10-09 11:47:55 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2010-07-17 14:27:28 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2010-07-17 12:29:04 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-07-13 05:23:32 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-10-27 23:18:17 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-07-13 05:23:32 +08:00
|
|
|
#include "llvm/MC/MCParser/MCAsmLexer.h"
|
2010-07-17 12:29:04 +08:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-01-23 12:28:49 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2010-07-13 05:23:32 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ELFAsmParser : public MCAsmParserExtension {
|
2010-07-19 06:22:07 +08:00
|
|
|
template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
|
|
|
|
void AddDirectiveHandler(StringRef Directive) {
|
|
|
|
getParser().AddDirectiveHandler(this, Directive,
|
|
|
|
HandleDirective<ELFAsmParser, Handler>);
|
|
|
|
}
|
|
|
|
|
2010-07-13 05:23:32 +08:00
|
|
|
bool ParseSectionSwitch(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, SectionKind Kind);
|
2011-02-23 00:53:11 +08:00
|
|
|
bool SeenIdent;
|
2010-07-13 05:23:32 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-25 05:59:22 +08:00
|
|
|
ELFAsmParser() : SeenIdent(false) {
|
|
|
|
BracketExpressionsSupported = true;
|
|
|
|
}
|
2010-07-13 05:23:32 +08:00
|
|
|
|
|
|
|
virtual void Initialize(MCAsmParser &Parser) {
|
|
|
|
// Call the base implementation.
|
|
|
|
this->MCAsmParserExtension::Initialize(Parser);
|
|
|
|
|
2010-07-19 06:22:07 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
|
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
|
2010-07-21 05:12:46 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
|
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
|
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
|
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
|
2011-07-26 01:11:29 +08:00
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
|
2010-07-19 06:22:07 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
|
2011-07-26 01:11:29 +08:00
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
|
2011-02-16 09:08:29 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
|
2010-07-19 06:22:07 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
|
2010-09-03 02:53:37 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
|
2010-10-09 11:47:55 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
|
2010-10-27 03:35:47 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
|
2010-10-27 23:18:17 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
|
2012-05-12 22:30:47 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
|
2010-11-01 22:28:48 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
|
2011-07-26 01:55:35 +08:00
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
|
|
|
|
AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
|
|
|
|
AddDirectiveHandler<
|
|
|
|
&ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
|
2010-07-13 05:23:32 +08:00
|
|
|
}
|
|
|
|
|
2010-09-16 05:48:40 +08:00
|
|
|
// FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
|
|
|
|
// the best way for us to get access to it?
|
2010-07-13 05:23:32 +08:00
|
|
|
bool ParseSectionDirectiveData(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_WRITE |ELF::SHF_ALLOC,
|
2010-07-13 05:23:32 +08:00
|
|
|
SectionKind::getDataRel());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveText(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_EXECINSTR |
|
|
|
|
ELF::SHF_ALLOC, SectionKind::getText());
|
2010-07-13 05:23:32 +08:00
|
|
|
}
|
2010-07-21 05:12:46 +08:00
|
|
|
bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_WRITE |
|
|
|
|
ELF::SHF_ALLOC, SectionKind::getBSS());
|
2010-07-21 05:12:46 +08:00
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getReadOnly());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveTData(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_TLS | ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getThreadData());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_TLS | ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getThreadBSS());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getDataRel());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getReadOnlyWithRel());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getReadOnlyWithRelLocal());
|
|
|
|
}
|
|
|
|
bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
|
2011-01-23 12:28:49 +08:00
|
|
|
return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_WRITE,
|
2010-07-21 05:12:46 +08:00
|
|
|
SectionKind::getDataRel());
|
|
|
|
}
|
2011-02-16 09:08:29 +08:00
|
|
|
bool ParseDirectivePushSection(StringRef, SMLoc);
|
|
|
|
bool ParseDirectivePopSection(StringRef, SMLoc);
|
2010-07-17 12:29:04 +08:00
|
|
|
bool ParseDirectiveSection(StringRef, SMLoc);
|
|
|
|
bool ParseDirectiveSize(StringRef, SMLoc);
|
2010-09-03 02:53:37 +08:00
|
|
|
bool ParseDirectivePrevious(StringRef, SMLoc);
|
2010-10-09 11:47:55 +08:00
|
|
|
bool ParseDirectiveType(StringRef, SMLoc);
|
2010-10-27 03:35:47 +08:00
|
|
|
bool ParseDirectiveIdent(StringRef, SMLoc);
|
2010-10-27 23:18:17 +08:00
|
|
|
bool ParseDirectiveSymver(StringRef, SMLoc);
|
2012-05-12 22:30:47 +08:00
|
|
|
bool ParseDirectiveVersion(StringRef, SMLoc);
|
2010-11-01 22:28:48 +08:00
|
|
|
bool ParseDirectiveWeakref(StringRef, SMLoc);
|
2011-07-26 01:55:35 +08:00
|
|
|
bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
|
2010-09-17 01:05:55 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool ParseSectionName(StringRef &SectionName);
|
2010-07-13 05:23:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-26 01:55:35 +08:00
|
|
|
/// ParseDirectiveSymbolAttribute
|
|
|
|
/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
|
|
|
|
bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
|
|
|
|
MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
|
|
|
|
.Case(".weak", MCSA_Weak)
|
|
|
|
.Case(".local", MCSA_Local)
|
|
|
|
.Case(".hidden", MCSA_Hidden)
|
|
|
|
.Case(".internal", MCSA_Internal)
|
|
|
|
.Case(".protected", MCSA_Protected)
|
|
|
|
.Default(MCSA_Invalid);
|
|
|
|
assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement)) {
|
|
|
|
for (;;) {
|
|
|
|
StringRef Name;
|
|
|
|
|
|
|
|
if (getParser().ParseIdentifier(Name))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
|
|
|
|
|
|
|
|
getStreamer().EmitSymbolAttribute(Sym, Attr);
|
|
|
|
|
|
|
|
if (getLexer().is(AsmToken::EndOfStatement))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-13 05:23:32 +08:00
|
|
|
bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, SectionKind Kind) {
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement))
|
|
|
|
return TokError("unexpected token in section switching directive");
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
getStreamer().SwitchSection(getContext().getELFSection(
|
|
|
|
Section, Type, Flags, Kind));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-17 12:29:04 +08:00
|
|
|
bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
|
2010-07-17 11:09:18 +08:00
|
|
|
StringRef Name;
|
|
|
|
if (getParser().ParseIdentifier(Name))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
const MCExpr *Expr;
|
|
|
|
if (getParser().ParseExpression(Expr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement))
|
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
|
|
|
|
getStreamer().EmitELFSize(Sym, Expr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-17 01:05:55 +08:00
|
|
|
bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
|
|
|
|
// A section name can contain -, so we cannot just use
|
|
|
|
// ParseIdentifier.
|
|
|
|
SMLoc FirstLoc = getLexer().getLoc();
|
|
|
|
unsigned Size = 0;
|
|
|
|
|
2011-01-25 02:02:54 +08:00
|
|
|
if (getLexer().is(AsmToken::String)) {
|
|
|
|
SectionName = getTok().getIdentifier();
|
|
|
|
Lex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-17 01:05:55 +08:00
|
|
|
for (;;) {
|
|
|
|
StringRef Tmp;
|
|
|
|
unsigned CurSize;
|
|
|
|
|
|
|
|
SMLoc PrevLoc = getLexer().getLoc();
|
|
|
|
if (getLexer().is(AsmToken::Minus)) {
|
|
|
|
CurSize = 1;
|
|
|
|
Lex(); // Consume the "-".
|
2011-01-25 02:02:54 +08:00
|
|
|
} else if (getLexer().is(AsmToken::String)) {
|
|
|
|
CurSize = getTok().getIdentifier().size() + 2;
|
|
|
|
Lex();
|
|
|
|
} else if (getLexer().is(AsmToken::Identifier)) {
|
|
|
|
CurSize = getTok().getIdentifier().size();
|
|
|
|
Lex();
|
|
|
|
} else {
|
2010-09-17 01:05:55 +08:00
|
|
|
break;
|
2011-01-25 02:02:54 +08:00
|
|
|
}
|
2010-09-17 01:05:55 +08:00
|
|
|
|
|
|
|
Size += CurSize;
|
|
|
|
SectionName = StringRef(FirstLoc.getPointer(), Size);
|
|
|
|
|
|
|
|
// Make sure the following token is adjacent.
|
|
|
|
if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Size == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-25 05:57:39 +08:00
|
|
|
static SectionKind computeSectionKind(unsigned Flags) {
|
2011-01-23 12:43:11 +08:00
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
2010-11-25 05:57:39 +08:00
|
|
|
return SectionKind::getText();
|
2011-01-23 12:43:11 +08:00
|
|
|
if (Flags & ELF::SHF_TLS)
|
2010-11-25 05:57:39 +08:00
|
|
|
return SectionKind::getThreadData();
|
|
|
|
return SectionKind::getDataRel();
|
|
|
|
}
|
|
|
|
|
2010-11-25 23:32:56 +08:00
|
|
|
static int parseSectionFlags(StringRef flagsStr) {
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < flagsStr.size(); i++) {
|
|
|
|
switch (flagsStr[i]) {
|
|
|
|
case 'a':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_ALLOC;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'x':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_EXECINSTR;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'w':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_WRITE;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'M':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_MERGE;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_STRINGS;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'T':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_TLS;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'c':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::XCORE_SHF_CP_SECTION;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::XCORE_SHF_DP_SECTION;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
case 'G':
|
2011-01-23 12:43:11 +08:00
|
|
|
flags |= ELF::SHF_GROUP;
|
2010-11-25 23:32:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2011-02-16 09:08:29 +08:00
|
|
|
bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
|
|
|
|
getStreamer().PushSection();
|
|
|
|
|
|
|
|
if (ParseDirectiveSection(s, loc)) {
|
|
|
|
getStreamer().PopSection();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
|
|
|
|
if (!getStreamer().PopSection())
|
|
|
|
return TokError(".popsection without corresponding .pushsection");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-17 12:29:04 +08:00
|
|
|
// FIXME: This is a work in progress.
|
|
|
|
bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
|
|
|
|
StringRef SectionName;
|
2010-09-17 01:05:55 +08:00
|
|
|
|
|
|
|
if (ParseSectionName(SectionName))
|
2010-07-17 12:29:04 +08:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
StringRef TypeName;
|
|
|
|
int64_t Size = 0;
|
2010-11-12 02:13:52 +08:00
|
|
|
StringRef GroupName;
|
2010-11-25 23:32:56 +08:00
|
|
|
unsigned Flags = 0;
|
|
|
|
|
|
|
|
// Set the defaults first.
|
|
|
|
if (SectionName == ".fini" || SectionName == ".init" ||
|
|
|
|
SectionName == ".rodata")
|
2011-01-23 12:43:11 +08:00
|
|
|
Flags |= ELF::SHF_ALLOC;
|
2010-11-25 23:32:56 +08:00
|
|
|
if (SectionName == ".fini" || SectionName == ".init")
|
2011-01-23 12:43:11 +08:00
|
|
|
Flags |= ELF::SHF_EXECINSTR;
|
2010-11-25 23:32:56 +08:00
|
|
|
|
2010-07-17 12:29:04 +08:00
|
|
|
if (getLexer().is(AsmToken::Comma)) {
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::String))
|
|
|
|
return TokError("expected string in directive");
|
|
|
|
|
2010-11-25 23:32:56 +08:00
|
|
|
StringRef FlagsStr = getTok().getStringContents();
|
2010-07-17 12:29:04 +08:00
|
|
|
Lex();
|
|
|
|
|
2010-11-25 23:32:56 +08:00
|
|
|
int extraFlags = parseSectionFlags(FlagsStr);
|
|
|
|
if (extraFlags < 0)
|
|
|
|
return TokError("unknown flag");
|
|
|
|
Flags |= extraFlags;
|
|
|
|
|
2011-01-23 12:43:11 +08:00
|
|
|
bool Mergeable = Flags & ELF::SHF_MERGE;
|
|
|
|
bool Group = Flags & ELF::SHF_GROUP;
|
2010-10-29 05:33:33 +08:00
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma)) {
|
|
|
|
if (Mergeable)
|
|
|
|
return TokError("Mergeable section must specify the type");
|
|
|
|
if (Group)
|
|
|
|
return TokError("Group section must specify the type");
|
|
|
|
} else {
|
|
|
|
Lex();
|
2010-11-12 23:47:08 +08:00
|
|
|
if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
|
|
|
|
return TokError("expected '@' or '%' before type");
|
2010-10-29 05:33:33 +08:00
|
|
|
|
2010-07-17 12:29:04 +08:00
|
|
|
Lex();
|
2010-10-29 05:33:33 +08:00
|
|
|
if (getParser().ParseIdentifier(TypeName))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
if (Mergeable) {
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("expected the entry size");
|
2010-07-17 12:29:04 +08:00
|
|
|
Lex();
|
2010-10-29 05:33:33 +08:00
|
|
|
if (getParser().ParseAbsoluteExpression(Size))
|
|
|
|
return true;
|
|
|
|
if (Size <= 0)
|
|
|
|
return TokError("entry size must be positive");
|
|
|
|
}
|
2010-07-17 12:29:04 +08:00
|
|
|
|
2010-10-29 05:33:33 +08:00
|
|
|
if (Group) {
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("expected group name");
|
|
|
|
Lex();
|
|
|
|
if (getParser().ParseIdentifier(GroupName))
|
|
|
|
return true;
|
2010-07-17 12:29:04 +08:00
|
|
|
if (getLexer().is(AsmToken::Comma)) {
|
|
|
|
Lex();
|
2010-10-29 05:33:33 +08:00
|
|
|
StringRef Linkage;
|
|
|
|
if (getParser().ParseIdentifier(Linkage))
|
2010-07-17 12:29:04 +08:00
|
|
|
return true;
|
2010-11-12 02:13:52 +08:00
|
|
|
if (Linkage != "comdat")
|
|
|
|
return TokError("Linkage must be 'comdat'");
|
2010-07-17 12:29:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement))
|
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
|
2011-01-23 12:28:49 +08:00
|
|
|
unsigned Type = ELF::SHT_PROGBITS;
|
2010-07-17 12:29:04 +08:00
|
|
|
|
|
|
|
if (!TypeName.empty()) {
|
|
|
|
if (TypeName == "init_array")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_INIT_ARRAY;
|
2010-07-17 12:29:04 +08:00
|
|
|
else if (TypeName == "fini_array")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_FINI_ARRAY;
|
2010-07-17 12:29:04 +08:00
|
|
|
else if (TypeName == "preinit_array")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_PREINIT_ARRAY;
|
2010-07-17 12:29:04 +08:00
|
|
|
else if (TypeName == "nobits")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_NOBITS;
|
2010-07-17 12:29:04 +08:00
|
|
|
else if (TypeName == "progbits")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_PROGBITS;
|
2010-12-27 05:30:59 +08:00
|
|
|
else if (TypeName == "note")
|
2011-01-23 12:28:49 +08:00
|
|
|
Type = ELF::SHT_NOTE;
|
2011-01-23 13:43:40 +08:00
|
|
|
else if (TypeName == "unwind")
|
|
|
|
Type = ELF::SHT_X86_64_UNWIND;
|
2010-07-17 12:29:04 +08:00
|
|
|
else
|
|
|
|
return TokError("unknown section type");
|
|
|
|
}
|
|
|
|
|
2010-11-25 05:57:39 +08:00
|
|
|
SectionKind Kind = computeSectionKind(Flags);
|
2010-07-17 12:29:04 +08:00
|
|
|
getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
|
2010-11-12 02:13:52 +08:00
|
|
|
Flags, Kind, Size,
|
|
|
|
GroupName));
|
2010-07-17 12:29:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-03 02:53:37 +08:00
|
|
|
bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
|
|
|
|
const MCSection *PreviousSection = getStreamer().getPreviousSection();
|
2011-02-16 09:08:29 +08:00
|
|
|
if (PreviousSection == NULL)
|
|
|
|
return TokError(".previous without corresponding .section");
|
|
|
|
getStreamer().SwitchSection(PreviousSection);
|
2010-09-03 02:53:37 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-09 11:47:55 +08:00
|
|
|
/// ParseDirectiveELFType
|
|
|
|
/// ::= .type identifier , @attribute
|
|
|
|
bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
|
|
|
|
StringRef Name;
|
|
|
|
if (getParser().ParseIdentifier(Name))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
// Handle the identifier as the key symbol.
|
|
|
|
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("unexpected token in '.type' directive");
|
|
|
|
Lex();
|
|
|
|
|
2010-11-12 23:47:08 +08:00
|
|
|
if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
|
|
|
|
return TokError("expected '@' or '%' before type");
|
2010-10-09 11:47:55 +08:00
|
|
|
Lex();
|
|
|
|
|
|
|
|
StringRef Type;
|
|
|
|
SMLoc TypeLoc;
|
|
|
|
|
|
|
|
TypeLoc = getLexer().getLoc();
|
|
|
|
if (getParser().ParseIdentifier(Type))
|
|
|
|
return TokError("expected symbol type in directive");
|
|
|
|
|
|
|
|
MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
|
|
|
|
.Case("function", MCSA_ELF_TypeFunction)
|
|
|
|
.Case("object", MCSA_ELF_TypeObject)
|
|
|
|
.Case("tls_object", MCSA_ELF_TypeTLS)
|
|
|
|
.Case("common", MCSA_ELF_TypeCommon)
|
|
|
|
.Case("notype", MCSA_ELF_TypeNoType)
|
2010-11-13 12:51:02 +08:00
|
|
|
.Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
|
2011-12-13 01:34:04 +08:00
|
|
|
.Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
|
2010-10-09 11:47:55 +08:00
|
|
|
.Default(MCSA_Invalid);
|
|
|
|
|
|
|
|
if (Attr == MCSA_Invalid)
|
|
|
|
return Error(TypeLoc, "unsupported attribute in '.type' directive");
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement))
|
|
|
|
return TokError("unexpected token in '.type' directive");
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
getStreamer().EmitSymbolAttribute(Sym, Attr);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-27 03:35:47 +08:00
|
|
|
/// ParseDirectiveIdent
|
|
|
|
/// ::= .ident string
|
|
|
|
bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
|
|
|
|
if (getLexer().isNot(AsmToken::String))
|
|
|
|
return TokError("unexpected token in '.ident' directive");
|
|
|
|
|
|
|
|
StringRef Data = getTok().getIdentifier();
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
const MCSection *Comment =
|
2011-01-23 12:28:49 +08:00
|
|
|
getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
|
2011-01-23 12:43:11 +08:00
|
|
|
ELF::SHF_MERGE |
|
|
|
|
ELF::SHF_STRINGS,
|
2010-10-27 03:35:47 +08:00
|
|
|
SectionKind::getReadOnly(),
|
2010-11-12 02:13:52 +08:00
|
|
|
1, "");
|
2010-10-27 03:35:47 +08:00
|
|
|
|
2011-02-16 09:08:29 +08:00
|
|
|
getStreamer().PushSection();
|
2010-10-27 03:35:47 +08:00
|
|
|
getStreamer().SwitchSection(Comment);
|
2011-02-23 00:53:11 +08:00
|
|
|
if (!SeenIdent) {
|
2010-10-27 03:35:47 +08:00
|
|
|
getStreamer().EmitIntValue(0, 1);
|
2011-02-23 00:53:11 +08:00
|
|
|
SeenIdent = true;
|
|
|
|
}
|
2010-10-27 03:35:47 +08:00
|
|
|
getStreamer().EmitBytes(Data, 0);
|
|
|
|
getStreamer().EmitIntValue(0, 1);
|
2011-02-16 09:08:29 +08:00
|
|
|
getStreamer().PopSection();
|
2010-10-27 03:35:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-27 23:18:17 +08:00
|
|
|
/// ParseDirectiveSymver
|
|
|
|
/// ::= .symver foo, bar2@zed
|
|
|
|
bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
|
|
|
|
StringRef Name;
|
|
|
|
if (getParser().ParseIdentifier(Name))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("expected a comma");
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
StringRef AliasName;
|
|
|
|
if (getParser().ParseIdentifier(AliasName))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
if (AliasName.find('@') == StringRef::npos)
|
|
|
|
return TokError("expected a '@' in the name");
|
|
|
|
|
|
|
|
MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
|
|
|
|
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
|
|
|
|
const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
|
|
|
|
|
|
|
|
getStreamer().EmitAssignment(Alias, Value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-12 22:30:47 +08:00
|
|
|
/// ParseDirectiveVersion
|
|
|
|
/// ::= .version string
|
|
|
|
bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
|
|
|
|
if (getLexer().isNot(AsmToken::String))
|
|
|
|
return TokError("unexpected token in '.version' directive");
|
|
|
|
|
|
|
|
StringRef Data = getTok().getIdentifier();
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
const MCSection *Note =
|
|
|
|
getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
|
|
|
|
SectionKind::getReadOnly());
|
|
|
|
|
|
|
|
getStreamer().PushSection();
|
|
|
|
getStreamer().SwitchSection(Note);
|
|
|
|
getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
|
|
|
|
getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
|
|
|
|
getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
|
|
|
|
getStreamer().EmitBytes(Data, 0); // name.
|
|
|
|
getStreamer().EmitIntValue(0, 1); // terminate the string.
|
|
|
|
getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
|
|
|
|
getStreamer().PopSection();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-01 22:28:48 +08:00
|
|
|
/// ParseDirectiveWeakref
|
|
|
|
/// ::= .weakref foo, bar
|
|
|
|
bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
|
|
|
|
// FIXME: Share code with the other alias building directives.
|
|
|
|
|
|
|
|
StringRef AliasName;
|
|
|
|
if (getParser().ParseIdentifier(AliasName))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return TokError("expected a comma");
|
|
|
|
|
|
|
|
Lex();
|
|
|
|
|
|
|
|
StringRef Name;
|
|
|
|
if (getParser().ParseIdentifier(Name))
|
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
|
|
|
|
|
|
|
|
MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
|
|
|
|
|
|
|
|
getStreamer().EmitWeakReference(Alias, Sym);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-13 05:23:32 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
MCAsmParserExtension *createELFAsmParser() {
|
|
|
|
return new ELFAsmParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|