Bug 17910 - Can't link with .lib (for dll) created by Visual Studio 2013 any more
Summary: Can't link with .lib (for dll) created by Visual Studio 2013 any more
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.25
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-31 13:44 UTC by Hannes Domani
Modified: 2023-03-21 22:48 UTC (History)
2 users (show)

See Also:
Host:
Target: i686-w64-mingw32
Build:
Last reconfirmed:


Attachments
.lib created by Visual Studio 2013 (498 bytes, application/x-archive)
2015-01-31 13:44 UTC, Hannes Domani
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Hannes Domani 2015-01-31 13:44:32 UTC
Created attachment 8092 [details]
.lib created by Visual Studio 2013

In 2.24 it was still possible to link with .lib files created by VS2013.
Now I get this message:

$ gcc -olib-user lib-user.c vc-dll-lib.lib
vc-dll-lib.lib: error adding symbols: File format not recognized
collect2.exe: error: ld returned 1 exit status


The vc-dll-lib.lib (32bit) is attached, and was created from this source:
#include <stdio.h>
__declspec(dllexport) int trythis(void)
{
	printf("output\n");
	return(1);
}


lib-user.c looks like this:
__declspec(dllimport) int trythis(void);
int main( void )
{
  return( trythis() );
}


I've found out this was introduced by this commit:
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=5a3f568b70bdfb91aacdfb66657b56d8c6d242f1

And the problem goes away if I revert a part like this:
--- bfd/coffgen.c	2014-12-23 09:47:10.000000000 +0100
+++ bfd/coffgen.c	2015-01-12 20:00:38.642229000 +0100
@@ -463,8 +463,6 @@
 	  if (strings == NULL)
 	    return NULL;
 	}
-      if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
-	return NULL;
       return strings + sym->_n._n_n._n_offset;
     }
 }

obj_coff_strings_len(abfd) is 0 in this example.
Comment 1 Sourceware Commits 2015-02-27 15:41:31 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit cdb602b17548eeebb5e700c507d734ea5f887a49
Author: Nick Clifton <nickc@redhat.com>
Date:   Fri Feb 27 15:38:32 2015 +0000

    Fixes a problem recognizing libraries created by Visual Studio.
    
    	PR binutils/17910
    	* coffgen.c (_bfd_coff_internal_syment_name): Only check for
    	string length overflow when the string table length is actually
    	set.
Comment 2 Nick Clifton 2015-02-27 15:44:09 UTC
Hi Domani,

  Sorry about that.  I have checked in a patch to fix the problem.  If the issue resurfaces, please reopenthis bug report.

Cheers
  Nick
Comment 3 Sourceware Commits 2023-03-21 22:48:46 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit 317e47ee5e7c539807d6cd16bde5c274025e8ee1
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Mar 22 09:05:10 2023 +1030

    PR17910 sym string offset check
    
    As far as I can see the only place that sets obj_coff_strings without
    setting obj_coff_strings_len is pe_ILF_build_a_bfd.  Fix that and we
    can simplify the sym string offset check.  This is just a tidy.
    pe_ILF_build_a_bfd doesn't create bad symbols and
    _bfd_coff_read_string_table will always result in non-zero
    obj_coff_strings_len when obj_coff_strings is non-NULL.
    
            PR 17910
            * coffgen.c (_bfd_coff_internal_syment_name): Always sanity
            check sym string offset.
            * peicode.h (pe_ILF_build_a_bfd): Set obj_coff_strings_len.