Bug 21500 - gdb sizeof(long int)=4 but gcc sizeof(long int)=8 on x86_64-pc-cygwin
Summary: gdb sizeof(long int)=4 but gcc sizeof(long int)=8 on x86_64-pc-cygwin
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: tdep (show other bugs)
Version: 7.10.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: good-first-bug
Depends on:
Blocks:
 
Reported: 2017-05-18 10:28 UTC by julian
Modified: 2020-03-16 21:01 UTC (History)
4 users (show)

See Also:
Host: CYGWIN_NT-10.0 Caroline-2 2.8.0(0.309/5/3) 2017-04-01 20:47 x86_64 Cygwin
Target: x86_64 Cygwin
Build:
Last reconfirmed: 2020-03-15 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description julian 2017-05-18 10:28:09 UTC
gcc sizeof(long)=8 but gdb sizeof(long)=4 on current x86_64-pc-cygwin. Consistency between gdb and gcc is required for any given platform.

The gdb build appears to be incorrect for x86_64-pc-cygwin. Tested with gcc 5.4.0 and gdb 7.10.1 (current cygwin versions) on x86_64-pc-cygwin running under Windows 10.


Please refer to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80777 for discussion whether gcc or gdb is incorrectly configured. In particular comment-8 there.

Also refer to https://bugzilla.redhat.com/show_bug.cgi?id=518712 which describes the same issue under Linux. Repeating the same test described in comment-1 there when run under x86_64-pc-cygwin gets the wrong answer:

$ gdb
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) show arch
The target architecture is set automatically (currently i386)
(gdb) set arch
Requires an argument. Valid arguments are i386, i386:x86-64, i386:x64-32, i8086, i386:intel, i386:x86-64:intel, i386:x64-32:intel, i386:nacl, i386:x86-64:nacl, i386:x64-32:nacl, auto.
(gdb) set arch i386:x86-64
The target architecture is assumed to be i386:x86-64
(gdb) print sizeof(long)
$1 = 4
(gdb) print sizeof(long long)
$2 = 8
(gdb) print sizeof(long*)
$3 = 8
(gdb) q
Comment 1 Pedro Alves 2017-05-18 12:29:15 UTC
The problem is that while Cygwin and MinGW ABIs are different, GDB assumes they're the same.

amd64-windows-tdep.c has:

  /* On Windows, "long"s are only 32bit.  */
  set_gdbarch_long_bit (gdbarch, 32);

and this gdbarch is used by both Cygwin and MinGW.

We need a new "show osabi" value for "Windows" (MinGW), and then come up with some way to figure out automatically from the program binary which of the ABIs is in effect.  Looking at the dll import list for the cygwin dll may be a good enough approximation.  (And add a hardcoded case for the cygwin dll itself.)
Comment 2 Simon Marchi 2020-03-15 17:56:32 UTC
I have a patch series in the pipeline which adds a new "Windows" OS ABI, to differentiate plain Windows binaries and Cygwin Windows binaries, and then addresses this issue.
Comment 3 Sourceware Commits 2020-03-16 20:58:51 UTC
The master branch has been updated by Simon Marchi <simark@sourceware.org>:

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

commit 30efb6c7af7ad8b50936157fe0a0ef22d6787dd7
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Mon Mar 16 16:56:36 2020 -0400

    gdb: define builtin long type to be 64 bits on amd64 Cygwin
    
    On Windows x86-64 (when building with MinGW), the size of the "long"
    type is 32 bits.  amd64_windows_init_abi therefore does:
    
        set_gdbarch_long_bit (gdbarch, 32);
    
    This is also used when the chosen OS ABI is Cygwin, where the "long"
    type is 64 bits.  GDB therefore gets sizeof(long) wrong when using the
    builtin long type:
    
        $ ./gdb -nx --data-directory=data-directory -batch -ex "set architecture i386:x86-64" -ex "set osabi Cygwin" -ex "print sizeof(long)"
        The target architecture is assumed to be i386:x86-64
        $1 = 4
    
    This patch makes GDB avoid setting the size of the long type to 32 bits
    when using the Cygwin OS ABI.  it will inherit the value set in
    amd64_init_abi.
    
    With this patch, I get:
    
        $ ./gdb -nx --data-directory=data-directory -batch -ex "set architecture i386:x86-64" -ex "set osabi Cygwin" -ex "print sizeof(long)"
        The target architecture is assumed to be i386:x86-64
        $1 = 8
    
    gdb/ChangeLog:
    
            PR gdb/21500
            * amd64-windows-tdep.c (amd64_windows_init_abi): Rename
            to...
            (amd64_windows_init_abi_common): ... this.  Don't set size of
            long type.
            (amd64_windows_init_abi): New function.
            (amd64_cygwin_init_abi): New function.
            (_initialize_amd64_windows_tdep): Use amd64_cygwin_init_abi for
            the Cygwin OS ABI.
            * i386-windows-tdep.c (_initialize_i386_windows_tdep): Clarify
            comment.
Comment 4 Simon Marchi 2020-03-16 21:01:25 UTC
Fixed by commit mentioned above.