0

Intel 32 ビット プロセッサで Linux 2.6.31-14 を使用しています。

C ファイル:

#include <stdio.h>

main()
{
    printf("Hello World!\n");
}

リンカー スクリプト:

SECTIONS{
    .text 0x00000100 :{
        *(.text)
    }
}

出力:

$ 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'

なにが問題ですか?リンカー スクリプトでダイナミック C ライブラリを使用するにはどうすればよいですか?

4

2 に答える 2

1

ld引数に-lcオプションを追加して、プログラムをC標準ライブラリ(libc.so)にリンクする必要があると思います。

ld -T linker.ld -lc -dynamic-linker /lib/ld-linux.so.2 -o test test.o

また、test.oにはプログラムエントリポイント(_startシンボル)がないため、プログラムの実行に問題が発生する可能性があります(セグメンテーション違反)。したがって、test.o内でmain()関数を呼び出し、exit()システム呼び出しを呼び出してコードの実行を終了するエントリポイントを持つ追加のオブジェクトファイルが必要になります。

これがstart.sコードです

# 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

次に、プログラムを作成する必要があります

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

また、この記事https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_freeをチェックして、Cコンパイラと標準ライブラリの動作の詳細を確認することもできます。

于 2012-07-01T07:52:24.117 に答える
0

あなたはcライブラリにリンクしていません。

私の64ビットシステムでは、次のとおりです。

-dynamic-linker /lib64/ld-linux-x86-64.so.2 /lib64/libc.so.6
于 2011-07-07T10:38:32.743 に答える