[v2] i386: Check invalid (%dx) usage

Message ID 20221109202129.475283-1-hjl.tools@gmail.com
State Accepted
Headers
Series [v2] i386: Check invalid (%dx) usage |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

H.J. Lu Nov. 9, 2022, 8:21 p.m. UTC
  (%dx) isn't a valid memory address in any modes.  It is used as a special
memory operand for input/output port address in AT&T syntax and should
only be used with input/output instructions.  Update i386_att_operand to
set i.input_output_operand to true for (%dx) and issue an error if (%dx)
is used with non-input/output instructions.

	PR gas/29751
	* config/tc-i386.c (_i386_insn): Add input_output_operand.
	(md_assemble): Issue an error if input/output memory operand is
	used with non-input/output instructions.
	(i386_att_operand): Set i.input_output_operand to true for
	(%dx).
	* testsuite/gas/i386/inval.l: Updated.
	* testsuite/gas/i386/x86-64-inval.l: Likewise.
	* testsuite/gas/i386/inval.s: Add tests for invalid (%dx) usage.
	* testsuite/gas/i386/x86-64-inval.s: Likewise.
---
 gas/config/tc-i386.c                  | 16 ++++++++++++++++
 gas/testsuite/gas/i386/inval.l        |  9 +++++++++
 gas/testsuite/gas/i386/inval.s        |  5 +++++
 gas/testsuite/gas/i386/x86-64-inval.l |  9 +++++++++
 gas/testsuite/gas/i386/x86-64-inval.s |  5 +++++
 5 files changed, 44 insertions(+)
  

Patch

diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index da005c5d61d..142b937423e 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -311,6 +311,10 @@  struct _i386_insn
     /* The operand to a branch insn indicates an absolute branch.  */
     bool jumpabsolute;
 
+    /* There is a memory operand of (%dx) which should be only used
+       with input/output instructions.  */
+    bool input_output_operand;
+
     /* Extended states.  */
     enum
       {
@@ -5045,6 +5049,17 @@  md_assemble (char *line)
       i.disp_operands = 0;
     }
 
+  /* The memory operand of (%dx) should be only used with input/output
+     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
+  if (i.input_output_operand
+      && ((i.tm.base_opcode | 0x82) != 0xee
+	  || i.tm.opcode_modifier.opcodespace != SPACE_BASE))
+    {
+      as_bad (_("input/output port address isn't allowed with `%s'"),
+	      i.tm.name);
+      return;
+    }
+
   if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
     optimize_encoding ();
 
@@ -11752,6 +11767,7 @@  i386_att_operand (char *operand_string)
 	  && !operand_type_check (i.types[this_operand], disp))
 	{
 	  i.types[this_operand] = i.base_reg->reg_type;
+	  i.input_output_operand = true;
 	  return 1;
 	}
 
diff --git a/gas/testsuite/gas/i386/inval.l b/gas/testsuite/gas/i386/inval.l
index 5d520f0e8e7..f66142ab171 100644
--- a/gas/testsuite/gas/i386/inval.l
+++ b/gas/testsuite/gas/i386/inval.l
@@ -99,6 +99,10 @@ 
 .*:115: Error: .*
 .*:116: Error: .*
 .*:117: Error: .*
+.*:119: Error: .*
+.*:120: Error: .*
+.*:121: Error: .*
+.*:122: Error: .*
 [ 	]*1[ 	]+\.text
 [ 	]*2[ 	]+\.allow_index_reg
 [ 	]*3[ 	]+\# All the following should be illegal
@@ -216,4 +220,9 @@ 
 [ 	]*[1-9][0-9]*[ 	]+inb	%dx, %ax
 [ 	]*[1-9][0-9]*[ 	]+outb	%ax, %dx
 [ 	]*[1-9][0-9]*[ 	]+movb	%ax, %bx
+[ 	]*[1-9][0-9]*[ 	]+
+[ 	]*[1-9][0-9]*[ 	]+incl	\(%dx\)
+[ 	]*[1-9][0-9]*[ 	]+incw	\(%dx\)
+[ 	]*[1-9][0-9]*[ 	]+mov	\(%dx\), %ax
+[ 	]*[1-9][0-9]*[ 	]+mov	%ax, \(%dx\)
 #pass
diff --git a/gas/testsuite/gas/i386/inval.s b/gas/testsuite/gas/i386/inval.s
index 4e9f751c61f..4e9a90de904 100644
--- a/gas/testsuite/gas/i386/inval.s
+++ b/gas/testsuite/gas/i386/inval.s
@@ -115,3 +115,8 @@  movnti word ptr [eax], ax
 	inb	%dx, %ax
 	outb	%ax, %dx
 	movb	%ax, %bx
+
+	incl	(%dx)
+	incw	(%dx)
+	mov	(%dx), %ax
+	mov	%ax, (%dx)
diff --git a/gas/testsuite/gas/i386/x86-64-inval.l b/gas/testsuite/gas/i386/x86-64-inval.l
index bbb8ba295cb..14b267421c4 100644
--- a/gas/testsuite/gas/i386/x86-64-inval.l
+++ b/gas/testsuite/gas/i386/x86-64-inval.l
@@ -111,6 +111,10 @@ 
 .*:117: Error: .*
 .*:118: Error: .*
 .*:121: Error: .*
+.*:123: Error: .*
+.*:124: Error: .*
+.*:125: Error: .*
+.*:126: Error: .*
 GAS LISTING .*
 
 
@@ -241,3 +245,8 @@  GAS LISTING .*
 [ 	]*[1-9][0-9]*[ 	]+
 [ 	]*[1-9][0-9]*[ 	]+\.att_syntax prefix
 [ 	]*[1-9][0-9]*[ 	]+movsd \(%rsi\), %ss:\(%rdi\), %ss:\(%rax\)
+[ 	]*[1-9][0-9]*[ 	]+
+[ 	]*[1-9][0-9]*[ 	]+incl	\(%dx\)
+[ 	]*[1-9][0-9]*[ 	]+incw	\(%dx\)
+[ 	]*[1-9][0-9]*[ 	]+mov	\(%dx\), %ax
+[ 	]*[1-9][0-9]*[ 	]+mov	%ax, \(%dx\)
diff --git a/gas/testsuite/gas/i386/x86-64-inval.s b/gas/testsuite/gas/i386/x86-64-inval.s
index 85c3582d4b2..7705ef1cccb 100644
--- a/gas/testsuite/gas/i386/x86-64-inval.s
+++ b/gas/testsuite/gas/i386/x86-64-inval.s
@@ -119,3 +119,8 @@  movnti word ptr [rax], ax
 
 	.att_syntax prefix
 	movsd (%rsi), %ss:(%rdi), %ss:(%rax)
+
+	incl	(%dx)
+	incw	(%dx)
+	mov	(%dx), %ax
+	mov	%ax, (%dx)