5

I am using Kali linux 64 bit and I am trying to execute the following programs from Dr. Paul carter's website. The gcc command is giving errors. What should I use in the gcc command?

nasm -f elf32 array1.asm
root@kali:assembly# gcc -o array1 array1.o array1c.c
array1c.c:9:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
array1c.c:10:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
/usr/bin/ld: i386 architecture of input file `array1.o' is    incompatible    with i386:x86-64 output
collect2: error: ld returned 1 exit status
Paul R
  • 208,748
  • 37
  • 389
  • 560
beegee Assem
  • 79
  • 1
  • 3
  • 16
  • Experimenting with assembly language as root is a terrible idea. http://apple.stackexchange.com/a/192422/118588 has a great explanation of the pitfalls of using root for everything, which applies to all Unix OSes, not just OS X. It's even worse when experimenting with ASM, since you might accidentally make an unintended system call that reboots your system, or worse. – Peter Cordes Jul 14 '15 at 04:01
  • See also http://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain/36901649#36901649 for a more complete guide to the various ways of building 32 or 64-bit binaries from asm source, with or without libc. – Peter Cordes Dec 19 '16 at 00:06

4 Answers4

7

You are attempting to link a 32 bit object file i386 to a 64 bit executable (i386:x86-64). Add -m32 to the gcc compilation line to create a 32 bit executable.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • adding -m32 did not work .. gcc -m32 array1.o -o array1 /usr/bin/ld: cannot find crt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No such file or directory /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc.a when searching for -lgcc /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find -lc ..... – beegee Assem Jul 14 '15 at 04:01
  • 2
    You need to install 32bit glibc development files for gcc to link with. – Peter Cordes Jul 14 '15 at 04:02
5
nasm -f elf64 array1.asm

then

ld -s -o array1 array1.o
4

First install this:

sudo apt-get install gcc-multilib g++-multilib

then Assemeble and link in this way:

nasm -f elf array1.asm -o array1.o

And finaly,

gcc -m32 array1.o -o array1.out

and run,

./array1.out

This should work......

Habibullah
  • 43
  • 8
1

(oops, I only skimmed the question, and thought you were making a standalone executable with just ld. See cad's answer for gcc -m32, for when you do want to link with libc and all that, rather than just try some little experiment as a standalone.)

You have to tell ld what machine you want the output to be for. It defaults to the native type.

nasm -f elf32 array1.asm  # or yasm
ld -m elf_i386 array1.o -o 32bit-array1

Unfortunately, a lot of asm guides / resources still have examples with 32bit x86 code.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Hi, thanks but I'm getting a whole new bunch of issues – beegee Assem Jul 14 '15 at 03:57
  • ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 array1.o: In function `init_loop': array1.asm:(.text+0x1d): undefined reference to `puts' array1.o: In function `Prompt_loop': array1.asm:(.text+0x37): undefined reference to `printf' array1.asm:(.text+0x46): undefined reference to `scanf' array1.asm:(.text+0x53): undefined reference to `dump_line' array1.o: In function `InputOK': array1.asm:(.text+0x6a): undefined reference to `printf' array1.asm:(.text+0x77): undefined reference to `puts' array1.o: In function `print_loop': – beegee Assem Jul 14 '15 at 03:58
  • Your code defines `main`, not `_start`, and uses library functions, so you just use gcc. My answer is only useful if you're writing a standalone program that makes its system calls directly. – Peter Cordes Jul 14 '15 at 04:05