www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/03/08/19:15:17

Date: 08 Mar 96 19:06:49 EST
From: "Troy D. Van Horn" <74617 DOT 365 AT compuserve DOT com>
To: DJGPP Mail List <djgpp AT delorie DOT com>
Subject: Packed Align bug in G++ 2.7.2??
Message-ID: <960309000648_74617.365_EHH43-1@CompuServe.COM>

HI,

     Last week, I downloaded DJGPP V2, and the first major program I compiled
which I wrote (a data compression program) failed to work.  I was using the
findfirst library function, and it was returning a nonsense value for the file
size.
     I looked at the source code for findfirst, and I could not see anything
wrong with it.  I then decided to try the _dos_findfirst function because It was
simpler because it did not try to handel LFNs.  I got the same nonsense results
with it.

      I just discovered the reason for the problem today--I was compiling the
program using C++.  I wrote a small test program and which uses the following
structure from <dos.h>, and it produced different offsets when I compile to
assembly depending on whether I use C or C++.

struct _find_t {      /* from dos.h */
  char reserved[21] __attribute__((packed));
  unsigned char attrib __attribute__((packed));
  unsigned short wr_time __attribute__((packed));
  unsigned short wr_date __attribute__((packed));
  unsigned long size __attribute__((packed));
  char name[256] __attribute__((packed));
};

Here is the test program:
--------------------
#include <dos.h>

void tfunc(struct find_t *file)
{
 file->reserved[0]='r';
 file->attrib='s';
 file->wr_time=23;
 file->wr_date=27;
 file->size=4788;
 file->name[0]='H';
}
---------------------
when compiled in C I get:
-----------------------
        .file   "fstest.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
        .align 2
.globl _tfunc
_tfunc:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%eax
        movb $114,(%eax)
        movl 8(%ebp),%eax
        movb $115,21(%eax)
        movl 8(%ebp),%eax
        movw $23,22(%eax)
        movl 8(%ebp),%eax
        movw $27,24(%eax)
        movl 8(%ebp),%eax
        movl $4788,26(%eax)        <-------Notice the offset of 26
                                           is correct...
        movl 8(%ebp),%eax
        movb $72,30(%eax)
L7:
        leave
        ret
--------------------
Now under C++ I get..
-------------------
        .file   "fstest.cc"
gcc2_compiled.:
___gnu_compiled_cplusplus:
.text
        .align 2
.globl _tfunc__FP7_find_t
_tfunc__FP7_find_t:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%eax
        movb $114,(%eax)
        movl 8(%ebp),%eax
        movb $115,21(%eax)
        movl 8(%ebp),%eax
        movw $23,22(%eax)
        movl 8(%ebp),%eax
        movw $27,24(%eax)
        movl 8(%ebp),%eax
        movl $4788,28(%eax) <-------- The offset is 28 here, It
                                       should be 26,
        movl 8(%ebp),%eax              But the compiler has aligned
                                        it on a dword
        movb $72,32(%eax)               boundary.
L9:
        leave
        ret
---------------------------
     Evendently the C++ compiler does not properly handel the
__attribute__((packed)) command.


Troy....    74617 DOT 365 AT compuserve DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019