Bug 22773 - [ARM] Invalid immediate constants produce incorrect instructions
Summary: [ARM] Invalid immediate constants produce incorrect instructions
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-02 09:01 UTC by Jeroen Koops
Modified: 2018-03-31 12:20 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeroen Koops 2018-02-02 09:01:29 UTC
I have the following bit of assembly:

    .syntax unified
    .cpu cortex-m4
    .thumb

    .section  .text

    orr r1, #12800
    orr r1, #12801

I invoke the GNU assembler as follows:

arm-none-eabi-gcc -g -Wall -c bla.s

The assembler issues no warnings, but the output, when inspected with objdump, looks as follows:

    bla.o:     file format elf32-littlearm


    Disassembly of section .text:

    00000000 <.text>:
       0:   f441 5148   orr.w   r1, r1, #12800  ; 0x3200
       4:   f243 2101   movw    r1, #12801  ; 0x3201

The second orr instruction, which should've produced an error since the constant #12801 cannot be encoded as an immediate, was silently changed into a mov instruction instead.

The exact version of the assembler is:
GNU assembler version 2.29.51 (arm-none-eabi) using BFD version (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 2.29.51.20171128

I'm running OSX:
Darwin trumposaurus.local 15.6.0 Darwin Kernel Version 15.6.0: Sun Jun  4 21:43:07 PDT 2017; root:xnu-3248.70.3~1/RELEASE_X86_64 x86_64
Comment 1 Jeroen Koops 2018-02-02 11:48:31 UTC
I highly suspect commit bada434212 to be the problem, in particular the fragment starting at tc-arm.c:23596.
Comment 2 Sourceware Commits 2018-02-13 16:51:15 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=db7bf1058d28b2b5e931c60435a13f6db15df6af

commit db7bf1058d28b2b5e931c60435a13f6db15df6af
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue Feb 13 16:50:04 2018 +0000

    Fix ARm assembler so that it rejects invalid immediate values for the Thumb ORR instruction.
    
    	PR 22773
    	* config/tc-arm.c (md_apply_fix): Test Rn field of Thumb ORR
    	instruction before assuming that it is a MOV instruction.
    	* testsuite/gas/arm/pr22773.s: New test.
    	* testsuite/gas/arm/pr22773.d: New test driver.
    	* testsuite/gas/arm/pr22773.l: New expected output.
Comment 3 Nick Clifton 2018-02-13 16:58:16 UTC
Hi Jeroen,

  Thanks for reporting this bug, and sorry for the delay in fixing it.

  The problem was indeed the code that you identified.  It was failing
  to check that the ORR instruction was actually a MOV instruction before
  it altered the encoding.

  I have checked in a small patch to fix this, added your test as a new
  testcase in the assembler testsuite and checked the result in to the
  mainstream sources.

Cheers
  Nick
Comment 4 Jeroen Koops 2018-02-14 07:51:59 UTC
Hi Nick, thanks for the fix!
Comment 5 Sourceware Commits 2018-03-29 11:32:26 UTC
The binutils-2_30-branch branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=763e359efaba5cc088604e3f3e1dda40fda06831

commit 763e359efaba5cc088604e3f3e1dda40fda06831
Author: Nick Clifton <nickc@redhat.com>
Date:   Thu Mar 29 12:31:04 2018 +0100

    Import patch from the mainline that fixes the ARM assembler's parsing of an illegal ORR instruction.
    
    	PR 22773
    	* config/tc-arm.c (md_apply_fix): Test Rn field of Thumb ORR
    	instruction before assuming that it is a MOV instruction.
    	* testsuite/gas/arm/pr22773.s: New test.
    	* testsuite/gas/arm/pr22773.d: New test driver.
    	* testsuite/gas/arm/pr22773.l: New expected output.