1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[MC] Add SMLoc to MCStreamer::emitSymbolAttribute and report changed binding warnings/errors for ELF

This commit is contained in:
Fangrui Song 2020-10-29 19:41:59 -07:00
parent c4068d104f
commit 2b0a4f95fd
20 changed files with 47 additions and 38 deletions

View File

@ -46,7 +46,8 @@ public:
void emitAssemblerFlag(MCAssemblerFlag Flag) override; void emitAssemblerFlag(MCAssemblerFlag Flag) override;
void emitThumbFunc(MCSymbol *Func) override; void emitThumbFunc(MCSymbol *Func) override;
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override; void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc Loc = SMLoc()) override;
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override; unsigned ByteAlignment) override;

View File

@ -508,8 +508,8 @@ public:
virtual void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); virtual void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
/// Add the given \p Attribute to \p Symbol. /// Add the given \p Attribute to \p Symbol.
virtual bool emitSymbolAttribute(MCSymbol *Symbol, virtual bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
MCSymbolAttr Attribute) = 0; SMLoc Loc = SMLoc()) = 0;
/// Set the \p DescValue for the \p Symbol. /// Set the \p DescValue for the \p Symbol.
/// ///

View File

@ -44,7 +44,8 @@ public:
void emitAssemblerFlag(MCAssemblerFlag Flag) override; void emitAssemblerFlag(MCAssemblerFlag Flag) override;
void emitThumbFunc(MCSymbol *Func) override; void emitThumbFunc(MCSymbol *Func) override;
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override; void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc) override;
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override; unsigned ByteAlignment) override;

View File

@ -43,7 +43,8 @@ public:
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override; void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
void emitAssemblerFlag(MCAssemblerFlag Flag) override; void emitAssemblerFlag(MCAssemblerFlag Flag) override;
void emitThumbFunc(MCSymbol *Func) override; void emitThumbFunc(MCSymbol *Func) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc Loc = SMLoc()) override;
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void BeginCOFFSymbolDef(MCSymbol const *Symbol) override; void BeginCOFFSymbolDef(MCSymbol const *Symbol) override;
void EmitCOFFSymbolStorageClass(int StorageClass) override; void EmitCOFFSymbolStorageClass(int StorageClass) override;

View File

@ -19,7 +19,8 @@ public:
std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCObjectWriter> OW,
std::unique_ptr<MCCodeEmitter> Emitter); std::unique_ptr<MCCodeEmitter> Emitter);
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc Loc = SMLoc()) override;
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override; unsigned ByteAlignment) override;
void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,

View File

@ -157,7 +157,8 @@ public:
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override; void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override; void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc) override;
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void BeginCOFFSymbolDef(const MCSymbol *Symbol) override; void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
@ -635,7 +636,7 @@ void MCAsmStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
} }
bool MCAsmStreamer::emitSymbolAttribute(MCSymbol *Symbol, bool MCAsmStreamer::emitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) { MCSymbolAttr Attribute, SMLoc) {
switch (Attribute) { switch (Attribute) {
case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute"); case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function

View File

@ -187,7 +187,8 @@ static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
return T2; return T2;
} }
bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute,
SMLoc Loc) {
auto *Symbol = cast<MCSymbolELF>(S); auto *Symbol = cast<MCSymbolELF>(S);
// Adding a symbol attribute always introduces the symbol, note that an // Adding a symbol attribute always introduces the symbol, note that an
@ -229,8 +230,8 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
// traditionally set the binding to STB_GLOBAL. This is error-prone, so we // traditionally set the binding to STB_GLOBAL. This is error-prone, so we
// error on such cases. Note, we also disallow changed binding from .local. // error on such cases. Note, we also disallow changed binding from .local.
if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL) if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL)
getContext().reportError(SMLoc(), Symbol->getName() + getContext().reportError(Loc, Symbol->getName() +
" changed binding to STB_GLOBAL"); " changed binding to STB_GLOBAL");
Symbol->setBinding(ELF::STB_GLOBAL); Symbol->setBinding(ELF::STB_GLOBAL);
Symbol->setExternal(true); Symbol->setExternal(true);
break; break;
@ -240,16 +241,16 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
// For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK. // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
// We emit a warning for now but may switch to an error in the future. // We emit a warning for now but may switch to an error in the future.
if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK) if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
getContext().reportWarning(SMLoc(), Symbol->getName() + getContext().reportWarning(Loc, Symbol->getName() +
" changed binding to STB_WEAK"); " changed binding to STB_WEAK");
Symbol->setBinding(ELF::STB_WEAK); Symbol->setBinding(ELF::STB_WEAK);
Symbol->setExternal(true); Symbol->setExternal(true);
break; break;
case MCSA_Local: case MCSA_Local:
if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL) if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL)
getContext().reportError(SMLoc(), Symbol->getName() + getContext().reportError(Loc, Symbol->getName() +
" changed binding to STB_LOCAL"); " changed binding to STB_LOCAL");
Symbol->setBinding(ELF::STB_LOCAL); Symbol->setBinding(ELF::STB_LOCAL);
Symbol->setExternal(false); Symbol->setExternal(false);
break; break;

View File

@ -93,7 +93,8 @@ public:
void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor, void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
unsigned Update, VersionTuple SDKVersion) override; unsigned Update, VersionTuple SDKVersion) override;
void emitThumbFunc(MCSymbol *Func) override; void emitThumbFunc(MCSymbol *Func) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc Loc = SMLoc()) override;
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override; unsigned ByteAlignment) override;
@ -290,8 +291,8 @@ void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
cast<MCSymbolMachO>(Symbol)->setThumbFunc(); cast<MCSymbolMachO>(Symbol)->setThumbFunc();
} }
bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym, bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym, MCSymbolAttr Attribute,
MCSymbolAttr Attribute) { SMLoc) {
MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym); MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
// Indirect symbols are handled differently, to match how 'as' handles // Indirect symbols are handled differently, to match how 'as' handles

View File

@ -25,8 +25,7 @@ namespace {
bool hasRawTextSupport() const override { return true; } bool hasRawTextSupport() const override { return true; }
void emitRawTextImpl(StringRef String) override {} void emitRawTextImpl(StringRef String) override {}
bool emitSymbolAttribute(MCSymbol *Symbol, bool emitSymbolAttribute(MCSymbol *, MCSymbolAttr, SMLoc) override {
MCSymbolAttr Attribute) override {
return true; return true;
} }

View File

@ -4881,7 +4881,7 @@ bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
if (Sym->isTemporary()) if (Sym->isTemporary())
return Error(Loc, "non-local symbol required"); return Error(Loc, "non-local symbol required");
if (!getStreamer().emitSymbolAttribute(Sym, Attr)) if (!getStreamer().emitSymbolAttribute(Sym, Attr, Loc))
return Error(Loc, "unable to emit symbol attribute"); return Error(Loc, "unable to emit symbol attribute");
return false; return false;
}; };

View File

@ -178,13 +178,13 @@ bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement)) { if (getLexer().isNot(AsmToken::EndOfStatement)) {
while (true) { while (true) {
StringRef Name; StringRef Name;
SMLoc Loc = getTok().getLoc();
if (getParser().parseIdentifier(Name)) if (getParser().parseIdentifier(Name))
return TokError("expected identifier in directive"); return TokError("expected identifier in directive");
MCSymbol *Sym = getContext().getOrCreateSymbol(Name); MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
getStreamer().emitSymbolAttribute(Sym, Attr); getStreamer().emitSymbolAttribute(Sym, Attr, Loc);
if (getLexer().is(AsmToken::EndOfStatement)) if (getLexer().is(AsmToken::EndOfStatement))
break; break;

View File

@ -77,7 +77,8 @@ void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
Alias->setVariableValue(Value); Alias->setVariableValue(Value);
} }
bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute,
SMLoc) {
assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported"); assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
auto *Symbol = cast<MCSymbolWasm>(S); auto *Symbol = cast<MCSymbolWasm>(S);

View File

@ -107,8 +107,8 @@ void MCWinCOFFStreamer::emitThumbFunc(MCSymbol *Func) {
llvm_unreachable("not implemented"); llvm_unreachable("not implemented");
} }
bool MCWinCOFFStreamer::emitSymbolAttribute(MCSymbol *S, bool MCWinCOFFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute,
MCSymbolAttr Attribute) { SMLoc) {
auto *Symbol = cast<MCSymbolCOFF>(S); auto *Symbol = cast<MCSymbolCOFF>(S);
getAssembler().registerSymbol(*Symbol); getAssembler().registerSymbol(*Symbol);

View File

@ -29,8 +29,8 @@ MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
: MCObjectStreamer(Context, std::move(MAB), std::move(OW), : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
std::move(Emitter)) {} std::move(Emitter)) {}
bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym, bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym, MCSymbolAttr Attribute,
MCSymbolAttr Attribute) { SMLoc) {
auto *Symbol = cast<MCSymbolXCOFF>(Sym); auto *Symbol = cast<MCSymbolXCOFF>(Sym);
getAssembler().registerSymbol(*Symbol); getAssembler().registerSymbol(*Symbol);

View File

@ -97,7 +97,7 @@ void RecordStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
} }
bool RecordStreamer::emitSymbolAttribute(MCSymbol *Symbol, bool RecordStreamer::emitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) { MCSymbolAttr Attribute, SMLoc) {
if (Attribute == MCSA_Global || Attribute == MCSA_Weak) if (Attribute == MCSA_Global || Attribute == MCSA_Weak)
markGlobal(*Symbol, Attribute); markGlobal(*Symbol, Attribute);
if (Attribute == MCSA_LazyReference) if (Attribute == MCSA_LazyReference)
@ -226,7 +226,7 @@ void RecordStreamer::flushSymverDirectives() {
// Don't use EmitAssignment override as it always marks alias as defined. // Don't use EmitAssignment override as it always marks alias as defined.
MCStreamer::emitAssignment(Alias, Value); MCStreamer::emitAssignment(Alias, Value);
if (Attr != MCSA_Invalid) if (Attr != MCSA_Invalid)
emitSymbolAttribute(Alias, Attr); emitSymbolAttribute(Alias, Attr, SMLoc());
} }
} }
} }

View File

@ -48,7 +48,8 @@ public:
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override; void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override; void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override; void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc Loc) override;
void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size, void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment, SMLoc Loc = SMLoc()) override; unsigned ByteAlignment, SMLoc Loc = SMLoc()) override;
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,

View File

@ -1,17 +1,17 @@
# RUN: not llvm-mc -filetype=obj -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error: # RUN: not llvm-mc -filetype=obj -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
# CHECK: error: local changed binding to STB_GLOBAL # CHECK: {{.*}}.s:[[#@LINE+3]]:8: error: local changed binding to STB_GLOBAL
local: local:
.local local .local local
.globl local .globl local
## `.globl x; .weak x` matches the GNU as behavior. We issue a warning for now. ## `.globl x; .weak x` matches the GNU as behavior. We issue a warning for now.
# CHECK: warning: global changed binding to STB_WEAK # CHECK: {{.*}}.s:[[#@LINE+3]]:7: warning: global changed binding to STB_WEAK
global: global:
.global global .global global
.weak global .weak global
# CHECK: error: weak changed binding to STB_LOCAL # CHECK: {{.*}}.s:[[#@LINE+3]]:8: error: weak changed binding to STB_LOCAL
weak: weak:
.weak weak .weak weak
.local weak .local weak

View File

@ -89,7 +89,8 @@ private:
// We only care about instructions, we don't implement this part of the API. // We only care about instructions, we don't implement this part of the API.
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override {} unsigned ByteAlignment) override {}
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute,
SMLoc) override {
return false; return false;
} }
void emitValueToAlignment(unsigned ByteAlignment, int64_t Value, void emitValueToAlignment(unsigned ByteAlignment, int64_t Value,

View File

@ -52,7 +52,7 @@ public:
Regions.addInstruction(Inst); Regions.addInstruction(Inst);
} }
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { bool emitSymbolAttribute(MCSymbol *, MCSymbolAttr, SMLoc) override {
return true; return true;
} }

View File

@ -28,8 +28,8 @@ public:
// These methods are pure virtual in MCStreamer, thus, have to be overridden: // These methods are pure virtual in MCStreamer, thus, have to be overridden:
MOCK_METHOD2(emitSymbolAttribute, MOCK_METHOD3(emitSymbolAttribute,
bool(MCSymbol *Symbol, MCSymbolAttr Attribute)); bool(MCSymbol *Symbol, MCSymbolAttr Attribute, SMLoc Loc));
MOCK_METHOD3(emitCommonSymbol, MOCK_METHOD3(emitCommonSymbol,
void(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment)); void(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment));
MOCK_METHOD5(emitZerofill, MOCK_METHOD5(emitZerofill,