1,105,214 Community Members

use x64 gcc to compile elf_i386 code

Member Avatar
NicAx64
Posting Pro
543 posts since Mar 2009
Reputation Points: 76 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 0 [?]
 
0
 

hi , I just using 64 bit linux with my powerfull desktop computer.

and I use Scientific linux as the destro,I use the RPM binary package to install
gcc. and I using nasm also.

I write simple example program in a nasm assembly (free) book.And the author
gives some library and include file for the assembly named 'asm_io.inc' and
'asm_io.o' so , they are linux 32 bit.

So what I need is to compile this to i386 targert. But my gcc doesn't allow this.
and I force the gcc linker using -m elf_i386 option. But it doesn't seems works.

[root@ZXDSL831CII ch01]# gcc -o first driver.o first.o  asm_io.o 
/usr/bin/ld: warning: i386 architecture of input file `first.o' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `asm_io.o' is incompatible with i386:x86-64 output
driver.o: In function `main':
driver.c:(.text+0xe): undefined reference to `asm_main'
collect2: ld returned 1 exit status
[root@ZXDSL831CII ch01]# nasm  -f aout first.asm
[root@ZXDSL831CII ch01]# gcc -o first driver.o first.o  asm_io.o 
/usr/bin/ld: warning: i386 architecture of input file `first.o' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `asm_io.o' is incompatible with i386:x86-64 output
[root@ZXDSL831CII ch01]# ./first
Illegal instruction  << see how much foolish is this gcc !
[root@ZXDSL831CII ch01]# gcc -m i386 -o first driver.o first.o  asm_io.o 
/usr/bin/ld: unrecognised emulation mode: i386
Supported emulations: elf_x86_64 elf_i386 i386linux
collect2: ld returned 1 exit status
[root@ZXDSL831CII ch01]# gcc -m i386linux -o first driver.o first.o  asm_io.o 
/usr/bin/ld: unrecognized option '--eh-frame-hdr'
/usr/bin/ld: use the --help option for usage information
collect2: ld returned 1 exit status
[root@ZXDSL831CII ch01]# gcc -m elf_i386 -o first driver.o first.o  asm_io.o 
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.1.2/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.1.2/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: ld returned 1 exit status
[root@ZXDSL831CII ch01]#

and I wonder how the 'first' is built without the error at first time.

yes it was a 64 bit executable file.

[root@ZXDSL831CII ch01]# file first
first: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
[root@ZXDSL831CII ch01]#

see ,

any ideas ! I mean without installing or building back the gcc from the scratch ?

Anyway looks like I have to download the source tarball gcc and change the configurations to i386 and build it back. Please give me some advice , I mean
the configure options.

and I need to keep the 64-bit gcc also , but need the 32 bit one also , please
give me some nice idea here.

--Thanks in advance--

Member Avatar
Stinomus
Junior Poster
109 posts since May 2009
Reputation Points: 11 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

The early attempts you've attemted to compile with the wrong architectural targets for your sources, it even warned you about that, obviously that's going to have issues.

The final attempt you've set the correct architecture but the version of libgcc it has found is incompatible (as the errors state). Those libraries are likely 64bit libraries. So is gcc looking in the correct place for the correct version of libgcc? Do you even have the 32bit libs installed? Obviously you cannot link 32bit targets to 64bit libs.

and I wonder how the 'first' is built without the error at first time.

Because - given the design of gcc - this is not illegal, it's probably not what you wanted and in that case it won't work but there are times when it is justifiable to do that. Hence it is a warning, not an error. Which is why you get an 'Illegal instruction' error when you try to run it.

Member Avatar
NicAx64
Posting Pro
543 posts since Mar 2009
Reputation Points: 76 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 0 [?]
 
0
 

oky , I think you got a point ,
Then I will try to override the library searching path to lib32 directory.

Member Avatar
Stinomus
Junior Poster
109 posts since May 2009
Reputation Points: 11 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

oky , I think you got a point ,
Then I will try to override the library searching path to lib32 directory.

If you have the correct libraries in there it should work.

Member Avatar
NicAx64
Posting Pro
543 posts since Mar 2009
Reputation Points: 76 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 0 [?]
 
0
 

I thinks I have dig enough threads right now.Need some help now.

can somebody give me some idea about how to install 32 bit libraries .
is there is a package to download ? or something article to read is very
usefull.

Member Avatar
Stinomus
Junior Poster
109 posts since May 2009
Reputation Points: 11 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

AFAIK there is a libgcc rpm that you could install - youll have to have a browse of the repo. Though the best option is to find the appropriate option to build the required 32bit versions of libraries when building gcc yourself.

Question Answered as of 4 Years Ago by Stinomus
Member Avatar
linuser
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@Stinomus Thank you. I installed libc6-dev-i386 (Ubuntu 64 bit) and it did the trick. I already had installed ia32-libs which did not solve above problem. After installing libc6-dev-i386 package, examples from pcasm-book.pdf executes. However I made some changes(don't know if it is required) to the make file(Makefile) of linux-ex.zip.

AS=nasm
ASFLAGS= -f elf32
CFLAGS=
CC=gcc -m32
CXX=g++ -m32
CXXFLAGS=

There are one or two examples that might not compile, but most do. And they are perfect 32-bit binary

$ file first
first: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article