From 6cb0a70ef31ff585f046cb20a30088c0433d84f0 Mon Sep 17 00:00:00 2001 From: Jan Beulich Date: Fri, 27 Dec 2019 09:39:17 +0100 Subject: [PATCH] x86-64: correct / adjust prefix emission First and foremost REX must come last. Next JumpInterSegment branches can't possibly have a REX prefix, as they're consistently CpuNo64. And finally make BND prefix handling in output_branch() consistent with that of other prefixes in the same function, and make its placement among prefixes consistent with output_jump() (which, oddly enough, still isn't the supposedly canonical order specified by the *_PREFIX definitions). --- gas/ChangeLog | 16 +++++++++ gas/config/tc-i386.c | 23 ++++++------- gas/testsuite/gas/i386/x86-64-branch-2.d | 10 +++--- gas/testsuite/gas/i386/x86-64-branch-2.s | 2 ++ gas/testsuite/gas/i386/x86-64-branch-3.d | 13 ++++--- gas/testsuite/gas/i386/x86-64-branch-3.s | 3 ++ gas/testsuite/gas/i386/x86-64-mpx-branch-1.d | 36 +++++++++++--------- gas/testsuite/gas/i386/x86-64-mpx-branch-1.s | 3 ++ gas/testsuite/gas/i386/x86-64-mpx-branch-2.d | 26 +------------- gas/testsuite/gas/i386/x86-64-mpx-branch-2.s | 3 ++ 10 files changed, 72 insertions(+), 63 deletions(-) diff --git a/gas/ChangeLog b/gas/ChangeLog index 578d76fe1aa..286f018747b 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,19 @@ +2019-12-27 Jan Beulich + + * config/tc-i386.c (output_branch): Handle BND prefix the same + way as other prefixes. Emit it last before REX. + (output_jump): Emit BND before REX. + (output_interseg_jump): Don't emit REX. + * testsuite/gas/i386/x86-64-branch-2.s, + testsuite/gas/i386/x86-64-branch-3.s, + testsuite/gas/i386/x86-64-mpx-branch-1.s, + testsuite/gas/i386/x86-64-mpx-branch-2.s: Add REX.W cases. + * testsuite/gas/i386/x86-64-mpx-branch-2.d: Match output against + x86-64-mpx-branch-1.d. + * testsuite/gas/i386/x86-64-branch-2.d, + testsuite/gas/i386/x86-64-branch-3.d, + testsuite/gas/i386/x86-64-mpx-branch-1.d: Adjust expectations. + 2019-12-27 Jan Beulich * config/tc-i386.c (flip_code16): New. diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c index 770fa527a06..8e89b00f8d1 100644 --- a/gas/config/tc-i386.c +++ b/gas/config/tc-i386.c @@ -7910,8 +7910,8 @@ output_branch (void) /* BND prefixed jump. */ if (i.prefix[BND_PREFIX] != 0) { - FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]); - i.prefixes -= 1; + prefix++; + i.prefixes--; } if (i.prefixes != 0 && !intel_syntax) @@ -7930,6 +7930,8 @@ output_branch (void) if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE) *p++ = i.prefix[SEG_PREFIX]; + if (i.prefix[BND_PREFIX] != 0) + *p++ = BND_PREFIX_OPCODE; if (i.prefix[REX_PREFIX] != 0) *p++ = i.prefix[REX_PREFIX]; *p = i.tm.base_opcode; @@ -8042,16 +8044,16 @@ output_jump (void) size = 2; } - if (i.prefix[REX_PREFIX] != 0) + /* BND prefixed jump. */ + if (i.prefix[BND_PREFIX] != 0) { - FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]); + FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]); i.prefixes -= 1; } - /* BND prefixed jump. */ - if (i.prefix[BND_PREFIX] != 0) + if (i.prefix[REX_PREFIX] != 0) { - FRAG_APPEND_1_CHAR (i.prefix[BND_PREFIX]); + FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]); i.prefixes -= 1; } @@ -8109,11 +8111,8 @@ output_interseg_jump (void) i.prefixes -= 1; code16 ^= CODE16; } - if (i.prefix[REX_PREFIX] != 0) - { - prefix++; - i.prefixes -= 1; - } + + gas_assert (!i.prefix[REX_PREFIX]); size = 4; if (code16) diff --git a/gas/testsuite/gas/i386/x86-64-branch-2.d b/gas/testsuite/gas/i386/x86-64-branch-2.d index 5078daa1977..196fa8ed8d6 100644 --- a/gas/testsuite/gas/i386/x86-64-branch-2.d +++ b/gas/testsuite/gas/i386/x86-64-branch-2.d @@ -6,10 +6,12 @@ Disassembly of section .text: -0+ : -[ ]*[a-f0-9]+: 66 e9 00 00 jmpw 4 2: R_X86_64_PC16 foo-0x2 +0+ : +[ ]*[a-f0-9]+: 66 e9 00 00 jmpw 4 2: R_X86_64_PC16 foo-0x2 +[ ]*[a-f0-9]+: 66 48 e9 00 00 00 00 data16 jmpq b 7: R_X86_64_PLT32 foo-0x4 -0+4 : +0+b : [ ]*[a-f0-9]+: 89 c3 mov %eax,%ebx -[ ]*[a-f0-9]+: 66 e8 00 00 callw a 8: R_X86_64_PC16 foo-0x2 +[ ]*[a-f0-9]+: 66 e8 00 00 callw 11 f: R_X86_64_PC16 foo-0x2 +[ ]*[a-f0-9]+: 66 48 e8 00 00 00 00 data16 callq 18 14: R_X86_64_PLT32 foo-0x4 #pass diff --git a/gas/testsuite/gas/i386/x86-64-branch-2.s b/gas/testsuite/gas/i386/x86-64-branch-2.s index 16c85a3f7b1..3a4911b67ce 100644 --- a/gas/testsuite/gas/i386/x86-64-branch-2.s +++ b/gas/testsuite/gas/i386/x86-64-branch-2.s @@ -1,7 +1,9 @@ .text data16 jmp foo + data16 rex.w jmp foo bar: mov %eax, %ebx data16 call foo + data16 rex.w call foo diff --git a/gas/testsuite/gas/i386/x86-64-branch-3.d b/gas/testsuite/gas/i386/x86-64-branch-3.d index c66647cd804..7b9e21dc661 100644 --- a/gas/testsuite/gas/i386/x86-64-branch-3.d +++ b/gas/testsuite/gas/i386/x86-64-branch-3.d @@ -6,11 +6,14 @@ Disassembly of section .text: -0+ : -[ ]*[a-f0-9]+: 66 e9 00 00 00 00 data16 jmpq 6 2: R_X86_64_PLT32 foo-0x4 +0+ : +[ ]*[a-f0-9]+: 66 e9 00 00 00 00 data16 jmpq 6 2: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: 66 48 e9 00 00 00 00 data16 rex\.W jmpq d 9: R_X86_64_PLT32 foo-0x4 -0+6 : +0+d : [ ]*[a-f0-9]+: 89 c3 mov %eax,%ebx -[ ]*[a-f0-9]+: 66 e8 00 00 00 00 data16 callq e a: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: 66 c7 f8 00 00 xbeginw 13 11: R_X86_64_PC16 foo-0x2 +[ ]*[a-f0-9]+: 66 e8 00 00 00 00 data16 callq 15 11: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: 66 48 e8 00 00 00 00 data16 rex\.W callq 1c 18: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: 66 c7 f8 00 00 xbeginw 21 1f: R_X86_64_PC16 foo-0x2 +[ ]*[a-f0-9]+: 66 48 c7 f8 00 00 00 00 data16 xbeginq 29 25: R_X86_64_PLT32 foo-0x4 #pass diff --git a/gas/testsuite/gas/i386/x86-64-branch-3.s b/gas/testsuite/gas/i386/x86-64-branch-3.s index 42bdac001fc..cc16487424b 100644 --- a/gas/testsuite/gas/i386/x86-64-branch-3.s +++ b/gas/testsuite/gas/i386/x86-64-branch-3.s @@ -1,9 +1,12 @@ .text data16 jmp foo + data16 rex.w jmp foo bar: mov %eax, %ebx data16 call foo + data16 rex.w call foo data16 xbegin foo + data16 rex.w xbegin foo diff --git a/gas/testsuite/gas/i386/x86-64-mpx-branch-1.d b/gas/testsuite/gas/i386/x86-64-mpx-branch-1.d index 7ee6c6b82fe..03e9675217d 100644 --- a/gas/testsuite/gas/i386/x86-64-mpx-branch-1.d +++ b/gas/testsuite/gas/i386/x86-64-mpx-branch-1.d @@ -8,22 +8,24 @@ Disassembly of section .text: -0+ : -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 6 2: R_X86_64_PC32 \*ABS\*\+0x10003c -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq c 8: R_X86_64_PC32 \*ABS\*\+0x10003c +0+ : +[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 6 2: R_X86_64_PC32 \*ABS\*\+0x10003c +[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq c 8: R_X86_64_PC32 \*ABS\*\+0x10003c +[ ]*[a-f0-9]+: 66 f2 48 e8 00 00 00 00 data16 bnd callq 14 10: R_X86_64_PC32 \*ABS\*\+0x10003c +[ ]*[a-f0-9]+: 66 f2 48 e9 00 00 00 00 data16 bnd jmpq 1c 18: R_X86_64_PC32 \*ABS\*\+0x10003c -0+c : -[ ]*[a-f0-9]+: f2 eb fd bnd jmp c -[ ]*[a-f0-9]+: f2 72 fa bnd jb c -[ ]*[a-f0-9]+: f2 e8 f4 ff ff ff bnd callq c -[ ]*[a-f0-9]+: f2 eb 09 bnd jmp 24 -[ ]*[a-f0-9]+: f2 72 06 bnd jb 24 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 24 +0+1c : +[ ]*[a-f0-9]+: f2 eb fd bnd jmp 1c +[ ]*[a-f0-9]+: f2 72 fa bnd jb 1c +[ ]*[a-f0-9]+: f2 e8 f4 ff ff ff bnd callq 1c +[ ]*[a-f0-9]+: f2 eb 09 bnd jmp 34 +[ ]*[a-f0-9]+: f2 72 06 bnd jb 34 +[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 34 -0+24 : -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 2a 26: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 31 2d: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 37 33: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 3d 39: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 44 40: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 4a 46: R_X86_64_PLT32 foo-0x4 +0+34 : +[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 3a 36: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 41 3d: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 47 43: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 4d 49: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 54 50: R_X86_64_PLT32 foo-0x4 +[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 5a 56: R_X86_64_PLT32 foo-0x4 diff --git a/gas/testsuite/gas/i386/x86-64-mpx-branch-1.s b/gas/testsuite/gas/i386/x86-64-mpx-branch-1.s index 3cdb1091724..16cd019ab4d 100644 --- a/gas/testsuite/gas/i386/x86-64-mpx-branch-1.s +++ b/gas/testsuite/gas/i386/x86-64-mpx-branch-1.s @@ -2,6 +2,9 @@ bnd call 0x100040 bnd jmp 0x100040 + bnd data16 rex.w call 0x100040 + bnd data16 rex.w jmp 0x100040 + foo1: bnd jmp foo1 bnd jb foo1 diff --git a/gas/testsuite/gas/i386/x86-64-mpx-branch-2.d b/gas/testsuite/gas/i386/x86-64-mpx-branch-2.d index 96e000936f1..c61b3af69e8 100644 --- a/gas/testsuite/gas/i386/x86-64-mpx-branch-2.d +++ b/gas/testsuite/gas/i386/x86-64-mpx-branch-2.d @@ -1,29 +1,5 @@ #as: -J -madd-bnd-prefix #objdump: -dwr #name: x86-64 branch with BND prefix +#dump: x86-64-mpx-branch-1.d #notarget: *-*-solaris* - -.*: +file format .* - - -Disassembly of section .text: - -0+ : -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 6 2: R_X86_64_PC32 \*ABS\*\+0x10003c -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq c 8: R_X86_64_PC32 \*ABS\*\+0x10003c - -0+c : -[ ]*[a-f0-9]+: f2 eb fd bnd jmp c -[ ]*[a-f0-9]+: f2 72 fa bnd jb c -[ ]*[a-f0-9]+: f2 e8 f4 ff ff ff bnd callq c -[ ]*[a-f0-9]+: f2 eb 09 bnd jmp 24 -[ ]*[a-f0-9]+: f2 72 06 bnd jb 24 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 24 - -0+24 : -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 2a 26: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 31 2d: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 37 33: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e9 00 00 00 00 bnd jmpq 3d 39: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 0f 82 00 00 00 00 bnd jb 44 40: R_X86_64_PLT32 foo-0x4 -[ ]*[a-f0-9]+: f2 e8 00 00 00 00 bnd callq 4a 46: R_X86_64_PLT32 foo-0x4 diff --git a/gas/testsuite/gas/i386/x86-64-mpx-branch-2.s b/gas/testsuite/gas/i386/x86-64-mpx-branch-2.s index 5fe90883a6e..15c53b0da19 100644 --- a/gas/testsuite/gas/i386/x86-64-mpx-branch-2.s +++ b/gas/testsuite/gas/i386/x86-64-mpx-branch-2.s @@ -2,6 +2,9 @@ call 0x100040 jmp 0x100040 + data16 rex.w call 0x100040 + data16 rex.w jmp 0x100040 + foo1: jmp foo1 jb foo1 -- 2.30.2