#943579 gdb: disassembles 16-bit code (i8086) wrong

Package:
gdb
Source:
gdb
Description:
GNU Debugger
Submitter:
Thorsten Glaser
Date:
2019-10-26 20:39:03 UTC
Severity:
normal
#943579#5
Date:
2019-10-26 20:24:28 UTC
From:
To:
I’m starting qemu-system-i386 with, for example, the MirBSD CD-ROM.
Then I attach gdb to qemu (started with -s -S), set a breakpoint at
the beginning of the bootsector, continue and disassemble.

(gdb) b *0x7c00
Breakpoint 1 at 0x7c00
(gdb) c
Continuing.

But it disassembles wrong:

(gdb) x/14i 0x7c00
=> 0x7c00:      xor    cx,cx
   0x7c03:      mov    ss,ecx
   0x7c05:      mov    esp,0x51667bfc
   0x7c0a:      popfw
   0x7c0c:      mov    es,ecx
   0x7c0e:      mov    edi,0xb1577c00
[…]
(gdb) x/14xb 0x7c00
0x7c00: 0x66    0x31    0xc9    0x8e    0xd1    0xbc    0xfc    0x7b
0x7c08: 0x66    0x51    0x66    0x9d    0x8e    0xc1
(gdb) show architecture
The target architecture is assumed to be i8086

The correct disassembly is:
	66 31 C9	XOR ECX,ECX
	8E D1		MOV SS,CX
	BC FC 7B	MOV SP,7BFCh
	66 51		PUSH ECX
	66 9D		POPFD
	8E C1		MOV ES,CX

Putting this code snippet into “objdump -d -Mintel,i8086” gets
it almost right (except for the popfd, which nasm also gets
wrong at least when assembling):

   0:   66 31 c9                xor    ecx,ecx
   3:   8e d1                   mov    ss,cx
   5:   bc fc 7b                mov    sp,0x7bfc
   8:   66 51                   push   ecx
   a:   66 9d                   popf
   c:   8e c1                   mov    es,cx

ndisasm *does* get it right:

00000000  6631C9            xor ecx,ecx
00000003  8ED1              mov ss,cx
00000005  BCFC7B            mov sp,0x7bfc
00000008  6651              push ecx
0000000A  669D              popfd
0000000C  8EC1              mov es,cx

#943579#10
Date:
2019-10-26 20:37:48 UTC
From:
To:
Control: notfound 943579 6.8-3

Interestingly enough this is an upstream regression.

Disassembling the same code (the ELF source) with
gdb 6.3.50.20050707 on MirBSD I get this:

(gdb) set disassembly-flavor intel
(gdb) set architecture i8086
The target architecture is assumed to be i8086
(gdb) disas _start
Dump of assembler code for function _start:
0x0000fe00 <_start+0>:  xor    ecx,ecx
0x0000fe03 <_start+3>:  movl   ss,cx
0x0000fe05 <_start+5>:  mov    sp,0x7bfc
0x0000fe08 <_start+8>:  push   ecx
0x0000fe0a <_start+10>: data32
0x0000fe0b <_start+11>: popf
0x0000fe0c <_start+12>: movl   es,cx
0x0000fe0e <_start+14>: mov    di,0x7c00
0x0000fe11 <_start+17>: push   di
[…]
(gdb) x/7i _start
0xfe00 <_start>:        xor    ecx,ecx
0xfe03 <_start+3>:      movl   ss,cx
0xfe05 <_start+5>:      mov    sp,0x7bfc
0xfe08 <_start+8>:      push   ecx
0xfe0a <_start+10>:     data32
0xfe0b <_start+11>:     popf
0xfe0c <_start+12>:     movl   es,cx

This is all as expected.

gdb 6.8-3 in Debian lenny (which I have a chroot at hand from)
behaves correctly as well. I do not know whether debugging from
a file instead of qemu is relevant as lenny’s gdb doesn’t handle
the gdbserver packets qemu sends.


bye,
//mirabilos