Mixing programming of assembly and C in linux - c

There are only two files, main.c and kernel.asm, and I tried to make a program with them using NASM and GCC. contents are as follows:
main.c
#include <stdio.h>
void Print_String() {
printf("Hello World!\n");
}
kernle.asm
extern Print_String
[section .text]
global _start
_start:
call Print_String
Compile and Link:
nasm -f elf -o kernel.o kernel.asm
gcc -c -o main.o main.c
ld -s -lc -o final kernel.o main.o
Then I run the final file with the command: ./final, but the result is depressing:
bash: ./final: No such file or directory
However, the current directory does have the file final, for the command ls, it displays:
final kernel.asm kernel.o main.c main.o
So why it cannot find the file final? Is there anything wrong? Any help appreciated!

It is not that it cannot find it, per se. The error message is somewhat misleading. The dynamic linker cannot resolve its dependencies, and thus your program image is not loadable (and not executable)
The problem is that you are dynamically linking against libc without any other of the paraphernalia to make dynamic linking actually work. Thus you are left with a binary image that cannot be loaded.
You might find that it's easier to statically link against libc. This can be done as follows:
ld -Bstatic -o final kernel.o main.o -lc
Notice you have to move the '-lc' bit after the code module main.o which uses it.
If you try this, you'll get a whole bunch of unresolved symbols. That's because you will also need to link against libgcc and libgcc_eh.
The following got me fairly close (apologies, working on a 64-bit system here):
ld -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/32/ -melf_i386 -Bstatic -lc -o final kernel.o main.o -lc -lgcc -lgcc_eh
This failed for me with
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/32//libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x193b): undefined reference to `dl_iterate_phdr'
Which doesn't make much sense. You may have more luck linking 32-bit on a 32-bit system.
Update
Apologies for the ramble above. I had a think about this again, and, of course, it is possible to make dynamic linking work. The missing piece is to specify the dynamic linker:
In my case, this was:
ld -dynamic-linker /lib32/ld-linux.so.2 -melf_i386 -o final kernel.o main.o -lc
So for you the following should work:
ld -dynamic-linker /lib/ld-linux.so.2 -o final kernel.o main.o -lc
Update again
In response to markzar's comment - you have to make a syscall to cleanly exit. This has the effect of doing something similar to exit(0) in C:
mov eax,1 ; Syscall #1
mov ebx,0 ; Return code 0 = success
int 80H

Try this. First change kernel.asm as follows:
extern Print_String
[section .text]
global main
main:
call Print_String
Then use the following commands to create the executable (instead of the linker).
nasm -f elf -o kernel.o kernel.asm
gcc -c -o main.o main.c
gcc -o final kernel.o main.o

Very simple: there is no main() to call in the program... thus whatever you do, the C-program-startup machinery doesn't get a hold.

Related

How to use ld to link compiled .o files [duplicate]

I'm trying to get my head around how the linking process works when producing an executable. To do that I'm reading Ian Taylor's blog series about it, but a lot of it is beyond me at the moment - so I'd like to see how it works in practice.
At the moment I produce some object files and link them via gcc with:
gcc -m32 -o test.o -c test.c
gcc -m32 -o main.o -c main.c
gcc -m32 -o test main.o test.o
How do I replicate the gcc -m32 -o test main.o test.o stage using ld?
I've tried a very naive: ld -A i386 ./test.o ./main.o
But that returns me these errors:
ld: i386 architecture of input file `./test.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `./main.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
./test.o: In function `print_hello':
test.c:(.text+0xd): undefined reference to `_GLOBAL_OFFSET_TABLE_'
test.c:(.text+0x1e): undefined reference to `puts'
./main.o: In function `main':
main.c:(.text+0x15): undefined reference to `_GLOBAL_OFFSET_TABLE_
I'm most confused by _start and _GLOBAL_OFFSET_TABLE_ being missing - what additional info does gcc give to ld to add them?
Here are the files:
main.c
#include "test.h"
void main()
{
print_hello();
}
test.h
void print_hello();
test.c
#include <stdio.h>
void print_hello()
{
puts("Hello, world");
}
#sam : I am not the best people to answer your question because I am a beginner in compilation. I know how to compile programs but I do not really understand all the details (https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools)
So, I decided this year to try to understand how compilation works and I tried to do, more or less, the same things as you tried a few days ago. As nobody has answered, I am going to expose what I have done but I hope an expert will supplement my answer.
Short answer : It is recommended to not use ld directly but to use gcc directly instead. Nevertheless, it is, as you write, interesting to know how the linking process works. This command works on my computer :
ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test test.o main.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o /usr/lib/crtn.o
Very Long answer :
How did I find the command above ?
As n.m suggested, run gcc with -v option.
gcc -v -m32 -o test main.o test.o
... /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 ... (many
options and parameters)....
If you run ld with these options and parameters (copy and paste), it should work.
Try your command with -m elf_i386 (cf. collect2 parameters)
ld -m elf_i386 test.o main.o
ld: warning: cannot find entry symbol _start; ....
Look for symbol _start in object files used in the full ld command.
readelf -s /usr/lib/crt1.o (or objdump -t)
Symbol table '.symtab' contains 18 entries: Num: Value Size
Type Bind Vis Ndx Name... 11: 00000000 0 FUNC
GLOBAL DEFAULT 2 _start
Add this object to your ld command :ld -m elf_i386 test.o main.o /usr/lib/crt1.o
... undefined reference to `__libc_csu_fini'...
Look for this new reference in object files. It is not so obvious to know which library/object files are used because of -L, -l options and some .so include other libraries. For example, cat /usr/lib/libc.so. But, ld with --trace option helps. Try this commandld --trace ... (collect2 parameters)At the end, you should findld -m elf_i386 -o test test.o main.o /usr/lib/crt1.o /usr/lib/libc_nonshared.a /lib/libc.so.6 /usr/lib/crti.oor shorter (cf. cat /usr/lib/libc.so) ld -m elf_i386 -o test test.o main.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o
It compiles but it does not run (Try to run ./test). It needs the right -dynamic-linker option because it is a dynamically linked ELF executable. (cf collect2 parameters to find it) ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test test.o main.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o But, it does not run (Segmentation fault (core dumped)) because you need the epilogue of the _init and _fini functions (https://gcc.gnu.org/onlinedocs/gccint/Initialization.html). Add the ctrn.o object. ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test test.o main.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o /usr/lib/crtn.o./test
Hello, world

I cannot run a ELF-format program .The shell tells me no such file or directory

My environment is Ubuntu 14 32bits.
I write three c files called main.c,foo.c,and bar.c respectively.
the codes are very simple.
The first source code is main.c
#include<stdio.h>
extern void foo();
int main(){
foo();
return 0;
}
the second source code is foo.c
#include<stdio.h>
void foo(){
printf("Hi,I am foo.");
bar();
}
the last one is bar.c
#include<stdio.h>
void bar(){
printf("Hi,I am bar.");
}
All the files above are be put into the same folder called test.
(its absolute path is /home/jack/Desktop/test)
then I issue the commands :
$ gcc -fPIC -shared -Wl,-soname,libbar.so.1 -o libbar.so.1.0.0 bar.c
$ ln -s libbar.so.1.0.0 libbar.so
$ gcc -fPIC -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.0 foo.c -lbar -L.
$ ln -s libfoo.so.1.0.0 libfoo.so
$ gcc -c main.c
$ ld -rpath /home/jack/Desktop/test -e main -o main main.o -L. -lfoo -lbar
then I run the executable file called main.
$./main
but the shell return the string below
bash: ./main: no such file or directory.
But the main file really exists in the current directory.
Why?
Normally you should not invoke ld directly. Instead you should use gcc to do the link. gcc passes some special options to the linker.
If I modify your script to use the correct sonames and to use gcc to link, it works. I did this:
gcc -fPIC -shared -Wl,-soname,libbar.so.1.0.0 -o libbar.so.1.0.0 bar.c
ln -s libbar.so.1.0.0 libbar.so
gcc -fPIC -shared -Wl,-soname,libfoo.so.1.0.0 -o libfoo.so.1.0.0 foo.c -lbar -L.
ln -s libfoo.so.1.0.0 libfoo.so
gcc -c main.c
gcc -Wl,-rpath,$(pwd) -o main main.o -L$(pwd) -lfoo -lbar
Using ld directly is for unusual situations. Don't do it. That said, offhand I do not know what as wrong with your approach. I think -e is wrong here -- you don't want to invoke main directly, _start does some other things. Looking at the output of ldd shows some important differences between the two approaches. Invoking the link using gcc -v will show you a bit of what is happening behind the scenes, in case you want to understand some more.
bash: ./main: no such file or directory.
But the main file really exists in the current directory.
Why?
The executable needs a suitable program interpreter to be able to perform dynamic linking. The no such file or directory message is printed because the default interpreter, which is wrong, can't be found.
Set it with the ld option -dynamic-linker /lib/ld-linux.so.2 (for 32 bit).
64 bit dynamic linker is /lib64/ld-linux-x86-64.so.2.
As your entry is directly main you must call exit(0) at the end of main. It can't be returned from main because noone has called it. The instruction pointer was directly set to the address of main by the kernel. Returning from main would probably result in a segfault and since you did not add a \n to the printf's the messages could not be seen.

Linking kernel - i386 architecture of input file is incompatible with i386:x86_64 output

I am trying to move on from my assembly file kernel stage to my C file kernel stage (finally...). But, I am having some trouble in the process of linking my compiled C kernel to my compiled assembly kernel entry program.
Here is the code for my kernel_entry.asm file.
[BITS 32] ; Starting in 32 bit protected mode
[EXTERN main] ; Extern to C file main function
call main ; Invoke main in our C kernel
jmp $ ; Jump here - Infinite loop
Here is the code for my kernel.c file.
void main() {
char* video_memory = (char*) 0xB8000;
*video_memory = 'X';
}
Here are the command lines I am using to compile them.
nasm -f elf -o kernel_entry.o kernel_entry.asm
gcc -ffreestanding -c kernel.c -o kernel.o
ld -o kernel.bin -Ttext 0x0500 kernel_entry.o kernel.o --oformat binary
The last command line gives me this error.
ld: i386 architecture of input file `kernel_entry.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 0000000000000500
Note: I am loading my kernel to the address and offset 0x0000:0x0500, which is why I use -Ttext 0x0500, I am unsure why the second ld warning appears but for now it seems unimportant (although if you offer any help in that regard it would be appreciated as well).
Can anyone tell me why I cannot link these files together? I am also running on Ubuntu dekstop 64 bit. Thank you in advance for any help you may give.
It looks as though you're compiling this code on a 64-bit system. As such, kernel.o is a 64-bit binary, and cannot be linked with the 32-bit kernel_entry.o.
Since you don't have any code in place to get the system into long mode, you probably want to compile the "kernel" as 32-bit code. Use -m32 to trigger this:
gcc -m32 -ffreestanding -c kernel.c -o kernel.o
^^^^

How to call c functions that call c standard library in nasm?

First I want to clarify that I know this question might have been answered hundreds of times. However after hours of Google search I simply couldn't find anything that's exactly what I want. Also even though I've been writing c programs for quite a while, I'm kind of new to nasm and ld. So I would really appreciate it if I can get a simple answer without having to read a whole nasm/ld tutorial or the complete manual.
What I want to do is:
say I have a function written in c that calls some function in the c standard library:
/* foo.c */
#include <stdio.h>
void foo(int i)
{
printf("%d\n", i);
}
I want to call this function in nasm so I tried this:
; main.asm
global _start
extern foo
section .text
_start:
push 1234567
call foo
add esp, 4
mov eax, 1
xor ebx, ebx
int 80h
Then I tried to compile them and run:
[user ~/Documents/asm/callc]#make all
nasm main.asm -felf
gcc -c foo.c -o foo.o -m32
ld -o main main.o foo.o -melf_i386 -lc
[user ~/Documents/asm/callc]#ls
foo.c foo.o main main.asm main.o Makefile
[user ~/Documents/asm/callc]#./main
bash: ./main: No such file or directory
[user ~/Documents/asm/callc]#bash main
main: main: cannot execute binary file
I didn't get any errors but apparently I couldn't run the executable output file.
If the c function doesn't call any library functions then the code above can be compiled and it will run without any problems. I also figured out a way to call library functions directly in nasm and use gcc to produce the final executable file. But none of them is exactly what I want.
EDIT:
1. I'm running 64-bit Ubuntu but I'm trying to write 32-bit programs so I used flags like -m32 and -melf_i386.
2. Output of file *:
[user ~/Documents/asm/sof]#file *
foo.c: C source, ASCII text
foo.c~: empty
foo.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
main.asm: C source, ASCII text
main.asm~: empty
main.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
Makefile: makefile script, ASCII text
Makefile~: makefile script, ASCII text
3. I really have no idea of how to tell ld to include the c standard library. I found something like -lglibc or -lc in some other posts. -lgibc doesn't work and -lc seems to be able to get rid of all errors and I probably thought it worked at first but maybe that's the problem since it probably doesn't link the correct library.
UPDATE
Adding -I/lib32/ld-linux.so.2 to the ld command solved my problem.
Below are commands to compile/assemble/link and run the program:
nasm main.asm -felf
gcc -c foo.c -o foo.o -m32
ld -o main main.o foo.o -melf_i386 -lc -I/lib32/ld-linux.so.2
./main
The C library provides code using the _start interface that starts the C runtime, calls main(), and shuts the runtime down. Hence if you intend to use the C library in your program you must not use the _start interface but provide a main() function.
This is the correct way to do it:
; main.asm
global main
extern foo
section .text
main:
push 1234567
call foo
add esp, 4
xor eax, eax
ret
Build with:
nasm -f elf32 -o main.o main.asm
gcc -m32 -o foo.o -c foo.c
gcc -m32 -o main main.o foo.o
Two remarks:
main() returns, instead of doing an exit system call, to allow the C runtime shutdown code to run.
gcc is used for linking. Internally gcc invokes ld with the appropriate parameters to link with the C library. These are platform specific and subject to change. Hence, don't use ld for this.

More GCC link time issues: undefined reference to main

I'm writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I'm making use of the GNU compilers and related toolchains; these tools are installed on the processor board (Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC (Windows) using WinSCP and the PuTTY terminal.
As usual I started with a simple C project having main.c and functions.s. I compile the main.c using GCC, assemble the functions.s using as and link the generated object files using once again GCC, but I get strange errors during this process.
An important finding -
Meanwhile, I found out that my assembly code may have some issues because when I individually assemble it using the command as -o functions.o functions.s and try running the generated functions.o using ./functions.o command, the Bash shell is failing to recognize this file as an executable (on pressing tab functions.o is not getting selected/PuTTY is not highlighting the file).
Can anyone suggest what's happening here? Are there any specific options I have to send, to GCC during the linking process? The errors I see are strange and beyond my understanding, I don't understand to what the GCC is referring.
I'm pasting here the contents of main.c, functions.s, the Makefile and the list of errors.
Help, please!!!
**Latest errors included after the makefile was edited as suggested here **
ubuntu#ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o hello main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
main.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
functions.s
* Main program */
.equ STACK_TOP, 0x20000800
.text
.global _start
.syntax unified
_start:
.word STACK_TOP, start
.type start, function
start:
movs r0, #10
movs r1, #0
.end
Makefile
all: hello
hello: main.o functions.o
gcc hello -o main.o functions.o
-- hello was included here after suggested here by guys at Stack Overflow, but the problem still persists; I still get the same errors.
main.o: main.c
gcc -c -mcpu=cortex-a8 main.c
functions.o: functions.s
as -mcpu=cortex-a8 -o functions.o functions.s
Errors
ubuntu#ubuntu-desktop:~/Documents/Project/Others/helloworld$ make
gcc -c -mcpu=cortex-a8 main.c
as -mcpu=cortex-a8 -o functions.o functions.s
gcc -o main.o functions.o
functions.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start':
init.c:(.text+0x30): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [hello] Error 1
In the makefile:
hello: main.o functions.o
gcc -o main.o functions.o
should be:
hello: main.o functions.o
gcc -o hello main.o functions.o
As it stands, you are linking functions.o, but not main.o, and producing an output executable called main.o, which is overwriting your existing main.o.
Shouldn't
hello: main.o functions.o
gcc -o main.o functions.o
be
hello: main.o functions.o
gcc -o hello main.o functions.o
As Bigbohne suggests, gcc is trying to link in the standard runtime library. Try adding the -nostdlib option to your gcc call:
gcc -nostdlib -o hello main.o functions.o
I think that has something to do with the Runtime library the gcc is linking at the end.
And in this library there already is a "_start".
I think you have to compile without "std library". but than you wont have printf,getchar and all the other useful stuff.
Normally execution of a program compiled with gcc starts in the function called _start (in a startup file provided by gcc), which calls main.
If you are supplying your own _start, you should pass -nostdlib to gcc when linking. This disables all default startup files and all default libraries. You should therefore also explicitly provide any libraries you need such as libc and libgcc.
Your _start however does not call main, so it is unclear why you bother to provide main and the .c file at all.
gcc -o main.o functions.o is a mistake, it should be gcc -o main main.o functions.o, but this will not fix the problem described above.

Resources