I am using Linux 2.6.31-14 on Intel 32-bit processor.
C file:
#include <stdio.h>
main()
{
printf("Hello World!\n");
}
Linker script:
SECTIONS{
.text 0x00000100 :{
*(.text)
}
}
Output:
$ gcc -S test.c
$ as -o test.o test.s
$ ld -T linker.ld -dynamic-linke开发者_开发百科r /lib/ld-linux.so.2 -o test test.o
test.o: In function `main':
test.c:(.text+0x11): undefined reference to `puts'
What is wrong? How can I make the linker script use the dynamic C library?
I think that you should link your program with C standard library (libc.so) by adding -lc option to ld arguments.
ld -T linker.ld -lc -dynamic-linker /lib/ld-linux.so.2 -o test test.o
Also you may have some problems with running your program (segmentation faults) because your test.o have no program entry point (_start symbol). So you will need additional object file with entry point that is calling your main() function inside test.o and than termitates code execution by calling exit() system call.
Here is start.s code
# Linux system calls constants
.equ SYSCALL_EXIT, 1
.equ INTERRUPT_LINUX_SYSCALL, 0x80
# Code section
.section .text
.globl _start
_start: # Program entry point
call main # Calling main function
# Now calling exit() system call
movl %eax, %ebx # Saving return value for exit() argument
movl $SYSCALL_EXIT, %eax # System call number
int $INTERRUPT_LINUX_SYSCALL # Raising programm interrupt
Then you should build your program
gcc test.c -S
as test.s -o test.o
as start.s -o start.o
ld start.o test.o -o test -lc --dynamic-linker=/lib/ld-linux.so.2
You may also want to check out this article https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_free to learn more about how C compiler and standard library works.
You are not linking in the c library.
On my 64bit system it's:
-dynamic-linker /lib64/ld-linux-x86-64.so.2 /lib64/libc.so.6
精彩评论