From 026cc2ff9552fa372bd5a38397fd0866d69fd96d Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 4 Dec 2018 10:10:50 +0000 Subject: [PATCH] [llvm-mc] - Do not crash when referencing undefined debug sections. MC has code that pre-creates few debug sections: https://github.com/llvm-mirror/llvm/blob/master/lib/MC/MCObjectFileInfo.cpp#L396 If users code has a reference to such section but does not redefine it, MC code currently asserts, because still thinks they are normally defined. The patch fixes the issue. Differential revision: https://reviews.llvm.org/D55173 llvm-svn: 348243 --- llvm/lib/MC/ELFObjectWriter.cpp | 14 ++++++++++++++ llvm/test/MC/ELF/undefined-debug.s | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 llvm/test/MC/ELF/undefined-debug.s diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 5b8b013..89f3b30 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -669,6 +669,20 @@ void ELFWriter::computeSymbolTable( } else { const MCSectionELF &Section = static_cast(Symbol.getSection()); + + // We may end up with a situation when section symbol is technically + // defined, but should not be. That happens because we explicitly + // pre-create few .debug_* sections to have accessors. + // And if these sections were not really defined in the code, but were + // referenced, we simply error out. + if (!Section.isRegistered()) { + assert(static_cast(Symbol).getType() == + ELF::STT_SECTION); + Ctx.reportError(SMLoc(), + "Undefined section reference: " + Symbol.getName()); + continue; + } + if (Mode == NonDwoOnly && isDwoSection(Section)) continue; MSD.SectionIndex = SectionIndexMap.lookup(&Section); diff --git a/llvm/test/MC/ELF/undefined-debug.s b/llvm/test/MC/ELF/undefined-debug.s new file mode 100644 index 0000000..95ead70 --- /dev/null +++ b/llvm/test/MC/ELF/undefined-debug.s @@ -0,0 +1,5 @@ +// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t 2>&1 | FileCheck %s +// CHECK: error: Undefined section reference: .debug_pubnames + +.section .foo,"",@progbits + .long .debug_pubnames -- 2.7.4