[llvm] 742040a - [MC] Properly report errors for .subsection
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 14 23:57:09 PDT 2023
Author: Fangrui Song
Date: 2023-06-14T23:57:03-07:00
New Revision: 742040a7f476302f2b582e44945d08f875fb3254
URL: https://github.com/llvm/llvm-project/commit/742040a7f476302f2b582e44945d08f875fb3254
DIFF: https://github.com/llvm/llvm-project/commit/742040a7f476302f2b582e44945d08f875fb3254.diff
LOG: [MC] Properly report errors for .subsection
For the out-of-range error, MCConstantExpr doesn't have a location, so we
can only show "<unknown>:0:".
Also, allow subsection numbers up to 2147483647, which is the maximum value GNU
assembler supports. (GNU assembler also supports negative numbers.)
Added:
Modified:
llvm/lib/MC/MCObjectStreamer.cpp
llvm/test/MC/ELF/subsection.s
Removed:
################################################################################
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 4c79df014a6c1..958ec48b94344 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -376,10 +376,15 @@ bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
int64_t IntSubsection = 0;
if (Subsection &&
- !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr()))
- report_fatal_error("Cannot evaluate subsection number");
- if (IntSubsection < 0 || IntSubsection > 8192)
- report_fatal_error("Subsection number out of range");
+ !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr())) {
+ getContext().reportError(Subsection->getLoc(),
+ "cannot evaluate subsection number");
+ }
+ if (!isUInt<31>(IntSubsection)) {
+ getContext().reportError(Subsection->getLoc(),
+ "subsection number must be within [0,2147483647]");
+ }
+
CurSubsectionIdx = unsigned(IntSubsection);
CurInsertionPoint =
Section->getSubsectionInsertionPoint(CurSubsectionIdx);
diff --git a/llvm/test/MC/ELF/subsection.s b/llvm/test/MC/ELF/subsection.s
index d437cacf63473..5381568d4dd1b 100644
--- a/llvm/test/MC/ELF/subsection.s
+++ b/llvm/test/MC/ELF/subsection.s
@@ -1,4 +1,5 @@
// RUN: llvm-mc -filetype=obj %s -o - -triple x86_64-pc-linux | llvm-objdump -s - | FileCheck %s
+// RUN: not llvm-mc -filetype=obj -triple x86_64 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
// CHECK: Contents of section .text:
// CHECK-NEXT: 0000 03042502 00000003 04250100 0000ebf7
@@ -35,3 +36,15 @@ l2:
.byte 2
.popsection
.byte 3
+
+
+.ifdef ERR
+// ERR: :[[#@LINE+1]]:13: error: cannot evaluate subsection number
+.subsection l2
+
+// ERR: <unknown>:0: error: subsection number must be within [0,2147483647]
+.subsection 0-1
+.subsection 2147483647
+// ERR: <unknown>:0: error: subsection number must be within [0,2147483647]
+.subsection 2147483648
+.endif
More information about the llvm-commits
mailing list