我认为您应该通过将 -lc 选项添加到 ld 参数来将您的程序与 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 编译器和标准库如何工作的更多信息。