From 549758f5225a941f7e1e4fac72fd205152c5ab1d Mon Sep 17 00:00:00 2001 From: Dmitry Preobrazhensky Date: Mon, 18 May 2020 14:04:58 +0300 Subject: [PATCH] [AMDGPU][MC] Corrected branch relocation handling to detect undefined labels Fixed ELF object writer to die gracefully when an undefined label is encountered in a branch instruction. See https://bugs.llvm.org/show_bug.cgi?id=41914. Reviewers: arsenm, rampitec Differential Revision: https://reviews.llvm.org/D79943 --- .../AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp | 11 +++++++++++ test/MC/AMDGPU/labels-branch-err.s | 4 ++++ 2 files changed, 15 insertions(+) create mode 100644 test/MC/AMDGPU/labels-branch-err.s diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp index d352219a7a9..619fde74e88 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp +++ b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp @@ -6,8 +6,10 @@ // //===----------------------------------------------------------------------===// +#include "AMDGPUFixupKinds.h" #include "AMDGPUMCTargetDesc.h" #include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCFixup.h" @@ -80,6 +82,15 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx, return ELF::R_AMDGPU_ABS64; } + if (Fixup.getTargetKind() == AMDGPU::fixup_si_sopp_br) { + const auto *SymA = Target.getSymA(); + assert(SymA); + + Ctx.reportError(Fixup.getLoc(), + Twine("undefined label '") + SymA->getSymbol().getName() + "'"); + return ELF::R_AMDGPU_NONE; + } + llvm_unreachable("unhandled relocation type"); } diff --git a/test/MC/AMDGPU/labels-branch-err.s b/test/MC/AMDGPU/labels-branch-err.s new file mode 100644 index 00000000000..e7e8bd114a9 --- /dev/null +++ b/test/MC/AMDGPU/labels-branch-err.s @@ -0,0 +1,4 @@ +// RUN: not llvm-mc -arch=amdgcn -filetype=obj -o /dev/null %s 2>&1 | FileCheck -check-prefix=ERROR %s +// ERROR: error: undefined label 'undef_label' + +s_branch undef_label