"file not recognized" when using the GNU linker

I’m probably doing something wrong as a newbie. Could you help me?

I wrote a simple Hello World program in C called hello.c and ran the following command:

gcc -S hello.c 

This produced hello.s . Then I used this file with the GNU assembler, as :

 as hello.s 

What caused the unexecutable a.out that still needs to be connected, I understand?

I am trying to bind it using ld , for example:

 ld a.out 

But get the following error:

 a.out: file not recognized: File truncated 

And ld deletes my file.

This is an x86 Ubuntu system. What am I doing wrong? Many thanks!

+7
linker-errors gnu-toolchain
source share
4 answers

My first question is: why are you compiling the code? If you want the assembler in a coded way, use gcc -S to get it (for viewing, I think).

But you do not need to run this via as to continue, just use:

 gcc -o hello hello.c gcc -S hello.c 

This first step will turn the C source directly into an executable file, the second will give you your assembler source.

Your specific problem may be that ld trying to write its output to a.out . If this is also your input file, it may be destroyed when ld starts. You can try renaming a.out to a.in before running the ld: ld a.in .

+6
source share

Here is how I do it:

 > gcc -S forums.c > as forums.s -o forums.o > gcc forums.o -o forums > ./forums test 

Why am I calling gcc instead of ld ? Because GCC takes care of binding the C runtime and executing other implementation-dependent materials. If you want to see this, use the --verbose option:

 > gcc --verbose forums.o -o forums Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++ --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic Thread model: posix gcc version 4.4.0 (GCC) COMPILER_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-o' 'forums' '-mtune=generic' /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o forums /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crt1.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crti.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtbegin.o -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../.. forums.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtend.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crtn.o 
+5
source share

EDIT: OK, I tried all of this on my system, and I think I know what the problem is. ld writes to a.out (its default output file) while reading it. Try something like this:

 ld a.out -o myprog 
+3
source share

reinstall glibc-devel anyway and you can check if it works. This process works for me.

+1
source share

All Articles