From 742040a7f476302f2b582e44945d08f875fb3254 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 14 Jun 2023 23:57:03 -0700 Subject: [PATCH] [MC] Properly report errors for .subsection For the out-of-range error, MCConstantExpr doesn't have a location, so we can only show ":0:". Also, allow subsection numbers up to 2147483647, which is the maximum value GNU assembler supports. (GNU assembler also supports negative numbers.) --- llvm/lib/MC/MCObjectStreamer.cpp | 13 +++++++++---- llvm/test/MC/ELF/subsection.s | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) 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: :0: error: subsection number must be within [0,2147483647] +.subsection 0-1 +.subsection 2147483647 +// ERR: :0: error: subsection number must be within [0,2147483647] +.subsection 2147483648 +.endif