ld --oformat binary
对于快速和肮脏的测试,你可以这样做:
as -o a.o a.S
ld --oformat binary -o a.out a.o
hd a.out
Gives:
00000000 90 90 |..|
00000002
不幸的是,这给出了一个警告:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400000
这没有多大意义binary
。它可以通过以下方式静音:
.section .text
.globl start
start:
nop
nop
and:
ld -e start --oformat binary -o a.out a.o
或者简单地使用:
ld -e 0 --oformat binary -o a.out a.o
这告诉ld
入口点不是_start
但地址处的代码0
.
遗憾的是,两者都没有as
nor ld
可以从 stdin / stdout 获取输入/输出,因此无需管道。
正确的引导扇区
如果您要做更严肃的事情,最好的方法是生成一个干净的最小链接器脚本。linker.ld
:
SECTIONS
{
. = 0x7c00;
.text :
{
*(.*)
. = 0x1FE;
SHORT(0xAA55)
}
}
在这里,我们还将魔术字节与链接描述文件一起放置。
链接描述文件对于控制重定位后的输出地址至关重要。了解有关搬迁的更多信息:https://stackoverflow.com/a/30507725/895245 https://stackoverflow.com/a/30507725/895245
将其用作:
as -o a.o a.S
ld --oformat binary -o a.img -T linker.ld a.o
然后你可以启动为:
qemu-system-i386 -hda a.img
此存储库上的工作示例:https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/Makefile
在 Binutils 2.24、Ubuntu 14.04 上测试。