[llvm] r348243 - [llvm-mc] - Do not crash when referencing undefined debug sections.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 4 02:10:50 PST 2018


Author: grimar
Date: Tue Dec  4 02:10:50 2018
New Revision: 348243

URL: http://llvm.org/viewvc/llvm-project?rev=348243&view=rev
Log:
[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

Added:
    llvm/trunk/test/MC/ELF/undefined-debug.s
Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=348243&r1=348242&r2=348243&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Tue Dec  4 02:10:50 2018
@@ -669,6 +669,20 @@ void ELFWriter::computeSymbolTable(
     } else {
       const MCSectionELF &Section =
           static_cast<const MCSectionELF &>(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<const MCSymbolELF &>(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);

Added: llvm/trunk/test/MC/ELF/undefined-debug.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/undefined-debug.s?rev=348243&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/undefined-debug.s (added)
+++ llvm/trunk/test/MC/ELF/undefined-debug.s Tue Dec  4 02:10:50 2018
@@ -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,"", at progbits
+  .long  .debug_pubnames




More information about the llvm-commits mailing list