c - 使用 ld 将目标文件链接到二进制文件时出现链接错误

标签 c assembly nasm ld

我正在学习操作系统教程。我可以成功编译代码,但在链接目标文件二进制文件时遇到问题。

ld: error: loader.o:1:1: invalid character

这是我的代码供您引用:

nasm 中的加载器

; Loader.asm
bits 32
extern _main
global start

start:
call _main  ; Call our kernel's main() function
cli        ; Stop interrupts (thats another article?)
hlt        ; Stop all instructions

我的C代码

//main.c
int main( void )
{
  puts("Hello, world!"); /* Print our welcome message */
 for(;;); /* Keep the OS running */
}


/* video.c */
int x, y; /* Our global 'x' and 'y' */
char color; /* Our global color attribute */
void putc( unsigned char c )
{
    char *vidmem = (char*)0xB8000; /* pointer to video memory */
    int pos = ( y * 2 ) + x; /* Get the position */
    vidmem[pos]   = c; /* print the character */
    vidmem[pos++] = color; /* Set the color attribute */
    if (c == '\n') // newline
    {
        y++;
        x = 0;
    }
    else
        x += 2; // 2 bytes per char
}

int puts( char *message )
{
    int length;
    while(*message)
    {
        putc(*message++);
        length++;
    }
return length;
}

我通过运行编译这些:

gcc -ffreestanding -fno-builtin -nostdlib -c *.c    // (that's main.c and video.c)
nasm -f aout loader.asm -o loader.o

最佳答案

您应该要求 nasm 创建 elf 输出,而不是 aout。即使用-f elf

关于c - 使用 ld 将目标文件链接到二进制文件时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19596917/

相关文章:

c# - C# 中是否有等效的限制

c - gcc -march= native 。检测为错误架构构建的二进制文件的方法?

c - 使用独立于 Unix 发行版的数据库/索引顺序文件

c++ - Windows MessageBox 忽略 WM_CLOSE

有人可以为上面的c程序编写汇编代码并将其转换为小于100字节的机器代码吗?

c - 这些无用的 assembly 线从何而来?

kernel - 如何从我的 16 位引导加载程序加载内核

linux - 使用 Assembly 在屏幕上打印字符

windows - NASM 教程使用 int 80h,但这不适用于 Windows

c - 当 C 代码编译成机器代码时,栈上无缘无故地保留了 20 个字节