【问题标题】:How to fix ld: cannot find kernel.bin: No such file or directory如何修复 ld:找不到 kernel.bin:没有这样的文件或目录
【发布时间】:2021-06-03 12:07:12
【问题描述】:

我正在尝试运行操作系统的实现,当我在终端上运行 make 时出现此错误,我使用的是 ubuntu 20.04。

这是生成文件:

C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h) 
# Nice syntax for file extension replacement
OBJ = ${C_SOURCES:.c=.o cpu/interrupt.o} 

# Change this if your cross-compiler is somewhere else
CC = /usr/local/i386elfgcc/bin/i386-elf-gcc
GDB = /usr/local/i386elfgcc/bin/i386-elf-gdb
# -g: Use debugging symbols in gcc
CFLAGS = -g -ffreestanding -Wall -Wextra -fno-exceptions -m32

# First rule is run by default
os-image.bin: boot/bootsect.bin kernel.bin
    cat $^ > os-image.bin

# '--oformat binary' deletes all symbols as a collateral, so we don't need
# to 'strip' them manually on this case
kernel.bin: boot/kernel_entry.o ${OBJ}
    ld -melf_i386 -o -no-PIE $@ -Ttext 0x1000 $^ --oformat binary

# Used for debugging purposes
kernel.elf: boot/kernel_entry.o ${OBJ}
    ld -melf_i386 -o -no-PIE $@ -Ttext 0x1000 $^ 

run: os-image.bin
    qemu-system-i386 -fda os-image.bin

# Open the connection to qemu and load our kernel-object file with symbols
debug: os-image.bin kernel.elf
    qemu-system-i386 -s -fda os-image.bin -d guest_errors,int &
    ${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"

# Generic rules for wildcards
# To make an object, always compile from its .c
%.o: %.c ${HEADERS}
    ${CC} ${CFLAGS} -c $< -o $@

%.o: %.asm
    nasm $< -f elf -o $@

%.bin: %.asm
    nasm $< -f bin -o $@

clean:
    rm -rf *.bin *.dis *.o os-image.bin *.elf
rm -rf kernel/*.o boot/*.bin drivers/*.o boot/*.o cpu/*.o libc/*.o

这是代码的来源,我无法安装教程中的交叉编译器。所以我尝试更改makefile以使用默认的gcc:

source

【问题讨论】:

  • 我觉得-o后面的文件名不见了:ld -melf_i386 -o kernel.bin -no-PIE $@ -Ttext 0x1000 $^ --oformat binary

标签: linux ld ubuntu-20.04


【解决方案1】:

您的链接器命令有问题,因为您放错了-no-PIE 选项。

不要写ld -o -no-PIE $@,而是写ld -o $@ -no-PIE

kernel.bin: boot/kernel_entry.o ${OBJ}
    ld -melf_i386 -o $@ -no-PIE -Ttext 0x1000 $^ --oformat binary

kernel.elf: boot/kernel_entry.o ${OBJ}
    ld -melf_i386 -o $@ -no-PIE -Ttext 0x1000 $^ 

$@ 是你的 make 命令的目标(这里是 kernel.binkernel.elf)。像你一样放错地方,你指示ld 将结果输出为一个名为-no-PIE 的文件,并使用kernel.bin(resp。kernel.elf)作为输入。因此,由于输入尚不存在,因此出现错误消息。

【讨论】:

    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2022-06-17
    • 1970-01-01
    • 2022-10-22
    相关资源
    最近更新 更多