0

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-linker /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?

1
  • I know this is very old, but you weren't hoping to create a DOS COM program from this? I only ask because 0x00000100 in the linker script suggests that may have been a possibility. Apr 2, 2018 at 15:54

2 Answers 2

1

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.

1
  • Sorry i didn't told anything about linker script but i think that problem is not in script but in libc library.
    – min0ru
    Jul 1, 2012 at 7:55
0

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
1
  • ld -T linker.ld -dynamic-linker /lib/ld-linux.so.2 /lib/libc.so.6 -o test test.o...Still not working
    – pflz
    Jul 7, 2011 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.