Skip to content

Commit

Permalink
[MC] Properly report errors for .subsection
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
MaskRay committed Jun 15, 2023
1 parent 2809308 commit 742040a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions llvm/lib/MC/MCObjectStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/MC/ELF/subsection.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 742040a

Please sign in to comment.