Subject: malign-double and stat()
To: None <port-i386@netbsd.org>
From: Christian Biere <christianbiere@gmx.de>
List: port-i386
Date: 10/26/2002 10:39:19
--N)F49yE'wj=.IAr+
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Hi,

I'm not sure whether I've encoutered a bug in gcc in the NetBSD sources or
no bug at all. If I compile the following program the structure st will be
wrong starting with st_size. This happens with gcc 2.95.3 and gcc 3.2,
too. FYI, this is the reason why stat() has to be replaced in
main/generator.c of /usr/pkgsrc/emulators/generator. man gcc(1) for gcc
3.2 warns:

Warning: if you use the -malign-double switch, structures
containing the above types will be aligned differently than the
published application binary interface specifications for the
386.

So must nobody use -malign-double or is there a problem with the
declaration of struct stat?

statf.c:
#include <stdio.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
  struct stat st;

  if (argc < 0)
    return 1;
  if (stat(argv[1], &st)) {
    perror("stat() failed");
    return 1;
  }
  printf("st.st_size = %lu\n", (unsigned long)st.st_size);
  return 0;
}

$ gcc -g -Wall -malign-double -o statf statf.c && ./statf statf
st.st_size = 0
$ gdb --quiet statf
(gdb) set args statf
(gdb) b 14
Breakpoint 1 at 0x8048ad4: file statf.c, line 14.
(gdb) r
Starting program: /tmp/statf statf
Breakpoint 1, main (argc=2, argv=0xbfbfb6cc) at statf.c:14
14        printf("st.st_size = %lu\n", (unsigned long)st.st_size);
(gdb) p st
$1 = {st_dev = 2817, st_ino = 557782, st_mode = 33216, st_nlink = 1, 
  st_uid = 1000, st_gid = 100, st_rdev = 2272232, st_atimespec = {
    tv_sec = 1035620246, tv_nsec = 341821000}, st_mtimespec = {
    tv_sec = 1035620244, tv_nsec = 341821000}, st_ctimespec = {
    tv_sec = 1035620244, tv_nsec = 0}, st_size = 128849018880, 
  st_blocks = 35184372088832, st_blksize = 0, st_flags = 0, 
  st_gen = 3559010112, st_qspare = {-3162156030863867904, 
    -4629781192386805761}}

$ gcc -Wall -o statf statf.c && ./statf statf
st.st_size = 6819

$ gcc -g -Wall -o statf statf.c && ./statf statf
st.st_size = 14631
$ gdb --quiet statf
(gdb) set args statf
(gdb) b 14
Breakpoint 1 at 0x8048ad4: file statf.c, line 14.
(gdb) r
Starting program: /tmp/statf statf
Breakpoint 1, main (argc=2, argv=0xbfbfb6cc) at statf.c:14
14        printf("st.st_size = %lu\n", (unsigned long)st.st_size);
(gdb) p st
$1 = {st_dev = 2817, st_ino = 557784, st_mode = 33216, st_nlink = 1, 
  st_uid = 1000, st_gid = 100, st_rdev = 2272240, st_atimespec = {
    tv_sec = 1035620544, tv_nsec = 341821000}, st_mtimespec = {
    tv_sec = 1035620536, tv_nsec = 341821000}, st_ctimespec = {
    tv_sec = 1035620536, tv_nsec = 0}, st_size = 14631, st_blocks = 30, 
  st_blksize = 8192, st_flags = 0, st_gen = 0, st_qspare = {
    -3177349388215111872, -3162156030863867904}}


Christian

--N)F49yE'wj=.IAr+
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (NetBSD)

iD8DBQE9ulTP0KQix3oyIMcRAn76AJ9Nvb/BAqHQAt+PBTtcwQdt4+xWiQCgsPpM
nTrvumYkeKmXkgaeAtYzyTI=
=OiXg
-----END PGP SIGNATURE-----

--N)F49yE'wj=.IAr+--