Skip to content

Commit

Permalink
[RISCV] Avoid overflow when determining number of nops for code align
Browse files Browse the repository at this point in the history
RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign() assumed that the
align specified would be greater than or equal to the minimum nop length, but
that is not always the case - for example if a user specifies ".align 0" in
assembly.

Differential Revision: https://reviews.llvm.org/D63274
Patch by Edward Jones.

llvm-svn: 366176
  • Loading branch information
asb committed Jul 16, 2019
1 parent e9ad0cf commit bb479ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,12 @@ bool RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign(
bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
unsigned MinNopLen = HasStdExtC ? 2 : 4;

Size = AF.getAlignment() - MinNopLen;
return true;
if (AF.getAlignment() <= MinNopLen) {
return false;
} else {
Size = AF.getAlignment() - MinNopLen;
return true;
}
}

// We need to insert R_RISCV_ALIGN relocation type to indicate the
Expand Down
7 changes: 7 additions & 0 deletions llvm/test/MC/RISCV/align.s
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ test:
ret
# NORELAX-RELOC-NOT: R_RISCV
# C-EXT-NORELAX-RELOC-NOT: R_RISCV
# Code alignment of a byte size less than the size of a nop must be treated
# as no alignment. This used to trigger a fatal error with relaxation enabled
# as the calculation to emit the worst-case sequence of nops would overflow.
.p2align 1
add a0, a0, a1
.p2align 0
add a0, a0, a1
# We only need to insert R_RISCV_ALIGN for code section
# when the linker relaxation enabled.
.data
Expand Down

0 comments on commit bb479ca

Please sign in to comment.