Chinaunix首页 | 论坛 | 博客
  • 博客访问: 437304
  • 博文数量: 123
  • 博客积分: 2686
  • 博客等级: 少校
  • 技术积分: 1349
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-23 22:11
文章分类
文章存档

2012年(3)

2011年(10)

2010年(100)

2009年(10)

我的朋友

分类:

2011-03-17 20:30:05

TOOLPREFIX =
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -fno-builtin -O2 -Wall -MD -ggdb -m32
#CFLAGS += -fno-stack-protector
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -m32
LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null)

qemu: bootblock
    dd if=/dev/zero of=mcore.img count=10000
    dd if=bootblock of=mcore.img conv=notrunc
    qemu -hda mcore.img -boot c

bootblock: bootasm.S bootmain.c
    $(CC) $(CFLAGS) -O -nostdinc -I. -c bootmain.c
    $(CC) $(CFLAGS) -nostdinc -I. -c bootasm.S
    $(LD) $(LDFLAGS) -N -e start -Ttext 0x7c00 -o bootblock.o bootasm.o bootmain.o
    $(OBJDUMP) -S bootblock.o > bootblock.asm
    $(OBJCOPY) -S -O binary bootblock.o bootblock
    ./sign.pl bootblock

clean:
    rm -f *.o bootblock *.d *.asm *.img


1. $(LD) $(LDFLAGS) --e start -Ttext 0x7c00 -o bootblock.bootasm.o bootmain.o

-N  Set the text and data sections to be readable and writable.

-e  entry.  Use entry as the explicit symbol for beginning execution of your program, rather than the default entry point.  If there is no symbol named entry, the linker will try to parse entry as a number,

-Ttext Locate a section in the output file at the absolute address given by org.

-o the output file name

上面命令的意思是:把bootasm.o 和 bootmain.o链接成bootblock.o, 并指定bootblock.0为可读可写,入口符合是start, 代码段的绝对起始地址是0x7c00.

2. $(OBJCOPY) --O binary bootblock.o bootblock

-S Do not copy relocation and symbol information from the source file.
-O bfdname. Write the output file using the object format bfdname.
即,copy bootblock.o到bootblock中,且不包含重定位和符合信息,且输出的格式是二进制。

#!/usr/bin/perl

open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";

$n = sysread(SIG, $buf, 1000);

if($n > 510){
  print STDERR "boot block too large: $n bytes (max 510)\n";
  exit 1;
}

print STDERR "boot block is $n bytes (max 510)\n";

$buf .= "\0" x (510-$n);
$buf .= "\x55\xAA";

open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
print SIG $buf;
close SIG;


阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~