views:

1915

answers:

3

I'm probably doing something wrong, being a newbie. Could you please help me out?

I've written a simple Hello World program in C called hello.c, and ran the following command:

gcc -S hello.c

That produced hello.s. Then I used that file with GNU assembler, as:

as hello.s

Which produced a non-executable a.out, which still needs to be linked, I understand?

I try to link it by using ld, like so:

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!

+1  A: 

EDIT: okay, I tried this all out on my system, and I think I know what the problem is. ld is writing to a.out (its default output file), while reading from it at the same time. Try something like this:

ld a.out -o myprog
Zifre
`a.out` can be any type of file. By calling `gcc` with the `-S` flag, it only outputs assembly, human-readable code. It doesn't assemble or link it. `as` most definitely shouldn't be linking it, that's `ld`'s job. The `-o` flag only allows you to change the filename of the result, whatever that result may be.
Yes .. but the point remains is that if you don't specify an output filename for ld the very first thing it will do is truncate a.out. Then it will look at its input files. If one of those is a.out it will spot that it has truncated it and fail. It then deletes the incomplete output file a.out.Use -o something on as and give that to ld. ld needs different input and output files, it can't link "in place".
pjc50
+1  A: 

My first question would be: why are you assembling the code? If you want the assembler code the, by all means, use gcc -S to get it (for viewing, I guess).

But you don't need to run that through as to keep going, just use:

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

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

Your specific problem may be that ld tries to write its output to a.out. If that's also your input file, it may well be being destroyed in the process of running ld. You could try renaming a.out to a.in before running the ld command: ld a.in.

paxdiablo
To answer your question, I'm guessing he's trying to learn assembly...
Zifre
I realise that, but I wish to use as and ld independently of gcc. I'm learning x86 assembly. The output of gcc -S helps me see how gcc implements various C programs in assembly. Then I can modify and play with that assembly code and finally assemble and link it myself, using as and ld. I realise gcc can assemble and link these files for me, but it's only using as and ld on my behalf. I wish to use as and ld directly.
+2  A: 

Here is how I do it:

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

Why do I invoke gcc instead of ld? Because GCC takes care of linking the C runtime, and doing other implementation-dependant stuff. If you want to see that, 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
Bastien Léonard