Fwd: Inconsistent warning/error message by GCC while linking 32/64 bit binary/library

vijay nag vijunag@gmail.com
Mon Dec 14 10:56:00 GMT 2015


Hello binutils,

I was working on a 32-64 bit porting project and while debugging a
crash I was very surprised to learn that GCC allowed linking of 64 bit
library against 32 bit binary with a warning.

If the classic "-L" compiler switch is used, GCC barfs out a "Skipping
incompatible xxxx.a" error and doesn't emit any executable. However,
It is interesting to note that if full path of the library is given
instead as part of compiler flag instead of "-L... -l" incantation,
GCC emitted the final executable with a warning.  So the question is,
does ELF specification allow interlinking of 32/64 bit obj files ? Why
are the compiler messages different in these two cases ? Why did the
compiler emit the executable in the first place instead of bailing out
?

Vijay bsd84:~/lib]# gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]

Vijay bsd84:~/lib]# cat a.h

#ifndef __a_h__
#define __a_h__

extern int init_a_h(void);

#endif /*__a_h__*/

Vijay bsd84:~/lib]# cat a.c

#include "a.h"

int init_a_h(void)
{
  return 4;
}

Vijay bsd84:~/lib]# cat b.c

#include "a.h"

int main()
{
  init_a_h();
}

Vijay bsd84:~/lib]#

Vijay bsd84:~/lib]# gcc -g -O0 a.c -c
Vijay bsd84:~/lib]# ar -rcs liba.a a.o
Vijay bsd84:~/lib]# readelf -a liba.a | grep ELF
ELF Header:
  Class:                             ELF64
Vijay bsd84:~/lib]# gcc -g -m32 b.c -c
Vijay bsd84:~/lib]# gcc -g -m32 b.o -L. -la
/usr/bin/ld: skipping incompatible ./liba.a when searching for -la
/usr/bin/ld: cannot find -la
Vijay bsd84:~/lib]# gcc -g -m32 b.o liba.a
/usr/bin/ld: warning: i386:x86-64 architecture of input file
`liba.a(a.o)' is incompatible with i386 output
Vijay bsd84:~/lib]# ls
a.c     a.h     a.o     a.out*  b.c     b.o     liba.a
Vijay bsd84:~/lib]#


Thanks,
Vijay Nag



More information about the Binutils mailing list