Bug 5105 - x86_64-pc-mingw32 native ld.exe randomly failed to read the archive file
Summary: x86_64-pc-mingw32 native ld.exe randomly failed to read the archive file
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.19
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-04 15:46 UTC by drangon
Modified: 2007-10-05 09:18 UTC (History)
2 users (show)

See Also:
Host: x86_64-pc-mingw32
Target: x86_64-pc-mingw32
Build: x86_64-redhat-linux-gnu
Last reconfirmed:


Attachments
modified bfd/archive.c, add some log print (16.94 KB, text/plain)
2007-10-04 15:48 UTC, drangon
Details
log output info when running the modified code (2.46 KB, text/plain)
2007-10-04 15:49 UTC, drangon
Details

Note You need to log in before you can comment on or make changes to this bug.
Description drangon 2007-10-04 15:46:07 UTC
under x86_64 linux, I built the cross x86_64-pc-mingw32 compiler using the
latest CVS/SVN head of binutils gcc mingw-w64,
and the use the cross compiler to build the native x86_64-pc-mingw32
compiler toolchain.

Then I ran the compiler in native WinXP x64, the compiler toolchain
failed to compile.
And I think this is mostly a bug of ld.exe when reading the archive file.

A very simple "hello world" program t3.c, use gcc.exe and as.exe to
create obj file :
..\target\bin\gcc -S t3.c
..\target\bin\as t3.s -o t3.o

then the following command can generate t3.exe successfully under command line

..\target\bin\ld.exe -static -o t3.exe
..\target\x86_64-pc-mingw32\lib\crt2.o
..\target\x86_64-pc-mingw32\lib\crtbegin.o   -L
..\target\x86_64-pc-mingw32\lib  t3.o t3_pp.o -lmingw32 -lmoldname
-lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32
-lmoldname -lmingwex -lmsvcrt
..\target\x86_64-pc-mingw32\lib\crtend.o

but if I double the "-lmingw32", it failed to generate exe file

..\target\bin\ld.exe -static -o t3.exe
..\target\x86_64-pc-mingw32\lib\crt2.o
..\target\x86_64-pc-mingw32\lib\crtbegin.o   -L
..\target\x86_64-pc-mingw32\lib  t3.o t3_pp.o -lmingw32 -lmingw32
-lmoldname -lmingwex -lmsvcrt
-luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lmoldname -lmingwex -lmsvcrt
 ..\target\x86_64-pc-mingw32\lib\crtend.o
..\target\x86_64-pc-mingw32\lib\libmingw32.a: file not recognized: File format n
ot recognized

If I run the command similar with the first successful example ( mainly change
the "\" to "/" ) in MSYS, run the same command several times,
all the odd times run successfule and all the even times run failed.
the same command!

drangon@DGN64 /c/tools/test2
$ /c/tools/target/bin/ld.exe -static -o t3.exe  /c/tools/target/x86_64-pc-mingw
32/lib/crt2.o   /c/tools/target/x86_64-pc-mingw32/lib/crtbegin.o   -L /c/tools/
target/lib.old/gcc/x86_64-pc-mingw32/4.3.0  t3.o t3_pp.o -lmingw32 -lmoldname -
lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lmoldname
 -lmingwex -lmsvcrt  /c/tools/target/x86_64-pc-mingw32/lib/crtend.o 

drangon@DGN64 /c/tools/test2
$ /c/tools/target/bin/ld.exe -static -o t3.exe  /c/tools/target/x86_64-pc-mingw
32/lib/crt2.o   /c/tools/target/x86_64-pc-mingw32/lib/crtbegin.o   -L /c/tools/
target/lib.old/gcc/x86_64-pc-mingw32/4.3.0  t3.o t3_pp.o -lmingw32 -lmoldname -
lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lmoldname
 -lmingwex -lmsvcrt  /c/tools/target/x86_64-pc-mingw32/lib/crtend.o 
c:\tools\target\bin\../x86_64-pc-mingw32/lib\libmsvcrt.a: file not recognized:
File format not recognized
Comment 1 drangon 2007-10-04 15:48:36 UTC
Created attachment 2033 [details]
modified bfd/archive.c, add some log print
Comment 2 drangon 2007-10-04 15:49:43 UTC
Created attachment 2034 [details]
log output info when running the modified code
Comment 3 drangon 2007-10-04 15:50:34 UTC
I add some printf in binutils/bfd/archive.c to print out some value.

----------------------------------------------------------------------
380   if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof
(struct ar_hdr))
 381     {
 382       if (bfd_get_error () != bfd_error_system_call)
 383     bfd_set_error (bfd_error_no_more_archived_files);
 384       return NULL;
 385     }
 386   printf( "-4- hdr.ar_mag [%s], ARFMAG [%s], mag [%s]\n",
hdr.ar_fmag, ARFMAG, mag == NULL ? "(null)" : mag );
 387   if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
 388       && (mag == NULL
 389       || strncmp (hdr.ar_fmag, mag, 2) != 0))
 390     {
 391       bfd_set_error (bfd_error_malformed_archive);
 392       return NULL;
 393     }
 394
 395   errno = 0;
 396   parsed_size = strtol (hdr.ar_size, NULL, 10);
 397   printf( "-4- hdr.ar_size [%s], parsed_size %d, ar_name [%s]\n",
hdr.ar_size, (int)parsed_size, hdr.ar_name );
 398   if (errno != 0)
 399     {
 400       bfd_set_error (bfd_error_malformed_archive);
 401       return NULL;
 402     }
---------------------------------------------------------------

and from the log, the first some times, it reads the archive successfully :

----------------------------------------------------------
*** [bfd_generic_archive_p:644] begin read file
[..\target\x86_64-pc-mingw32\lib\libmingw32.a]
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [1758      `
], parsed_size 1758, ar_name [/               1191037575  0     0
0       1758      `
]
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [22        `
], parsed_size 22, ar_name [//
    22        `
]
-2- slurp ok
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [22        `
], parsed_size 22, ar_name [//
    22        `
]
-3- extended ok
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [2985      `
], parsed_size 2985, ar_name [crt0_c.o/       1191037555  500   501
100664  2985      `
]
*** has map and first object failed.
-----------------------------------------------------------

but the last some times, it reads failed

------------------------------------------------------------
*** [bfd_generic_archive_p:644] begin read file
[..\target\x86_64-pc-mingw32\lib\libmingw32.a]
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [1758      `
], parsed_size 1758, ar_name [/               1191037575  0     0
0       1758      `
]
-4- hdr.ar_mag [`
], ARFMAG [`
], mag [(null)]
-4- hdr.ar_size [22        `
], parsed_size 22, ar_name [//
    22        `
]
-2- slurp ok
-4- hdr.ar_mag [ `], ARFMAG [`
], mag [(null)]
-3- namedata is NULL
*** slurp or extend failed
----------------------------------------------------------

the hdr head reads " hdr.ar_mag [ `] ", it should be "`\012",
I don't understand why this value suddenly goes wrong ...
Comment 4 H.J. Lu 2007-10-05 02:29:30 UTC
Does

# ld.exe -r --whole-archive ..\target\x86_64-pc-mingw32\lib\libmingw32.a

work?
Comment 5 drangon 2007-10-05 03:43:05 UTC
Subject: Re:  x86_64-pc-mingw32 native ld.exe randomly failed to read the archive file

your command works.

I add some log in bfd_seek() and bfd_pread(),
it seems that the file offset suddenly minus by 1,
this is not caused by a seek(), it seem like some call return -1 and
then add to position.



5 Oct 2007 02:29:30 -0000, hjl at lucon dot org
<sourceware-bugzilla@sourceware.org>:
>
> ------- Additional Comments From hjl at lucon dot org  2007-10-05 02:29 -------
> Does
>
> # ld.exe -r --whole-archive ..\target\x86_64-pc-mingw32\lib\libmingw32.a
>
> work?
>
> --
>
>
> http://sourceware.org/bugzilla/show_bug.cgi?id=5105
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
>
Comment 6 H.J. Lu 2007-10-05 04:48:44 UTC
(In reply to comment #5)
> Subject: Re:  x86_64-pc-mingw32 native ld.exe randomly failed to read the
archive file
> 
> your command works.
> 
> I add some log in bfd_seek() and bfd_pread(),
> it seems that the file offset suddenly minus by 1,
> this is not caused by a seek(), it seem like some call return -1 and
> then add to position.
> 

It may be an interface or mingw issue. Please check if types used
in bfd_seek and bfd_bread are correct.
Comment 7 drangon 2007-10-05 08:14:06 UTC
Subject: Re:  x86_64-pc-mingw32 native ld.exe randomly failed to read the archive file

After some more log, I think this is a bug of mingw-crt,
After some special read, the "ftello64()" will has one less than "ftell()"

the code fragment is :

--------------------------------------
243 static file_ptr
244 cache_btell (struct bfd *abfd)
245 {
246   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
247   if (f == NULL)
248     return abfd->where;
249   printf( "-6- btell begin, file %p, abfd->where %d ( %d, %d )\n",
f, (int)abfd->where, (int)ftell( f ), (int)ftello64( f ) );
250   int ret = real_ftell( f );
251   printf( "-6- btell end, file %p, abfd->where %d ( %d, %d ), ret
%d\n", f, (int)abfd->where, (int)ftell( f ), (int)ftello64( f ), ret
);
252   return ret;
253   // return real_ftell (f);
254 }
255
256 static int
257 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
258 {
259   FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
260   if (f == NULL)
261     return -1;
262   printf( "-6- bseek begin, file %p, abfd->where %d ( %d, %d ),
off %lld\n", f, (int)abfd->where, (int)ftell( f ), (int)ftello64( f ),
(long long)offset
    );
263   int ret = real_fseek (f, offset, whence);
264   printf( "-6- bseek snd, file %p, abfd->where %d ( %d, %d ), ret
%d\n", f, (int)abfd->where, (int)ftell( f ), (int)ftello64( f ), ret
);
265   return ret;
266   // return real_fseek (f, offset, whence);
267 }
------------------------------------

and the log output is :

-------------------------------------------
*** [bfd_generic_archive_p:645] begin read file
[..\target\x86_64-pc-mingw32\lib\libmingw32.a]
-5- read begin, abfd->where 8 ( 8 ), want 16 bytes
-5- read end, 16 bytes, got 16, abfd->where 24 ( 24 )
-5- seek begin, abfd->where 24 ( 24 ), arg pos -16
-6- bseek begin, file 000007FF7FC792F0, abfd->where 24 ( 24, 24 ), off -16
-6- bseek snd, file 000007FF7FC792F0, abfd->where 24 ( 8, 8 ), ret 0
-5- file_position -16, position -16, abfd->origin 0, result 0
-5- seek end, abfd->where 8 ( 8 )
-4- file pos 8
-5- read begin, abfd->where 8 ( 8 ), want 60 bytes
-5- read end, 60 bytes, got 60, abfd->where 68 ( 68 )
-4- parsed_size 1758, ar_name [47]
-5- read begin, abfd->where 68 ( 68 ), want 4 bytes
-5- read end, 4 bytes, got 4, abfd->where 72 ( 72 )
-5- read begin, abfd->where 72 ( 72 ), want 376 bytes
-5- read end, 376 bytes, got 376, abfd->where 448 ( 448 )
-5- read begin, abfd->where 448 ( 448 ), want 1378 bytes
-5- read end, 1378 bytes, got 1378, abfd->where 1826 ( 1826 )
-5- bfd_tell begin, abfd->where 1826 ( 1826 )
-6- btell begin, file 000007FF7FC792F0, abfd->where 1826 ( 1826, 1825 )
-6- btell end, file 000007FF7FC792F0, abfd->where 1826 ( 1826, 1825 ), ret 1825
-5- bfd_tell end, abfd->where 1825 ( 1826 ), ptr 1825, abfd->origin 0
-5- seek begin, abfd->where 1825 ( 1826 ), arg pos 1826
-6- bseek begin, file 000007FF7FC792F0, abfd->where 1825 ( 1826, 1825
), off 1826
-6- bseek snd, file 000007FF7FC792F0, abfd->where 1825 ( 1826, 1826 ), ret 0
-5- file_position 1826, position 1826, abfd->origin 0, result 0
-5- seek end, abfd->where 1826 ( 1826 )
-4- file pos 1826
-5- read begin, abfd->where 1826 ( 1826 ), want 60 bytes
-5- read end, 60 bytes, got 60, abfd->where 1886 ( 1886 )
-4- parsed_size 22, ar_name [47]
-2- slurp ok
-5- seek begin, abfd->where 1886 ( 1886 ), arg pos 1826
-6- bseek begin, file 000007FF7FC792F0, abfd->where 1886 ( 1886, 1886
), off 1826
-6- bseek snd, file 000007FF7FC792F0, abfd->where 1886 ( 1826, 1826 ), ret 0
-5- file_position 1826, position 1826, abfd->origin 0, result 0
-5- seek end, abfd->where 1826 ( 1826 )
-5- read begin, abfd->where 1826 ( 1826 ), want 16 bytes
-5- read end, 16 bytes, got 16, abfd->where 1842 ( 1842 )
-5- seek begin, abfd->where 1842 ( 1842 ), arg pos -16
-6- bseek begin, file 000007FF7FC792F0, abfd->where 1842 ( 1842, 1841 ), off -16
-6- bseek snd, file 000007FF7FC792F0, abfd->where 1842 ( 1825, 1825 ), ret 0
-5- file_position -16, position -16, abfd->origin 0, result 0
-5- seek end, abfd->where 1826 ( 1825 )
-4- file pos 1825
-5- read begin, abfd->where 1826 ( 1825 ), want 60 bytes
-5- read end, 60 bytes, got 60, abfd->where 1886 ( 1885 )
-3- namedata is NULL
*** slurp or extend failed
----------------------------------------------------

5 Oct 2007 04:48:44 -0000, hjl at lucon dot org
<sourceware-bugzilla@sourceware.org>:
>
> ------- Additional Comments From hjl at lucon dot org  2007-10-05 04:48 -------
> (In reply to comment #5)
> > Subject: Re:  x86_64-pc-mingw32 native ld.exe randomly failed to read the
> archive file
> >
> > your command works.
> >
> > I add some log in bfd_seek() and bfd_pread(),
> > it seems that the file offset suddenly minus by 1,
> > this is not caused by a seek(), it seem like some call return -1 and
> > then add to position.
> >
>
> It may be an interface or mingw issue. Please check if types used
> in bfd_seek and bfd_bread are correct.
>
> --
>
>
> http://sourceware.org/bugzilla/show_bug.cgi?id=5105
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
>
Comment 8 drangon 2007-10-05 09:18:01 UTC
this is a bug of mingw-w64 crt, not binutils