操作系统引导部分见boot目录下的三个汇编文件。
在linux1.0中Makefile文件也只有300行,但是到了linux2.6就有了1345行,这里的主要问题是linux2.6支持了多种CPU,而linux1.0还只是支持intel 8086系列。Linux1.0的Makefile文件如下:
VERSION = 1 !版本号
PATCHLEVEL = 0 !补丁号
ALPHA =
all: Version zImage !Version用于生成.config和.depend文件,zImage就是要
#生成的核心,Version依赖于dummy,作用是删除tools/version.h和如果系统中没有.config
#和.depend文件的话,帮助生成这两个文件,zImage要生成的映像文件名,1.0版本的核心
#不支持非压缩的核心
.EXPORT_ALL_VARIABLES: #导出所有的值,用于生成.config文件时,读取config.in文件
CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; /
else if [ -x /bin/bash ]; then echo /bin/bash; /
else echo sh; fi ; fi)
#
# Make "config" the default target if there is no configuration file or
# "depend" the target if there is no top-level dependency information.
# 如果没有configuration文件make config将会是缺省的目标,如果没有顶层的依赖信
!息.depend将会是缺省的目标。
ifeq (.config,$(wildcard .config)) !如果既包含了.config文件,也包含了.depend文件,
include .config !则include它们
ifeq (.depend,$(wildcard .depend)) !如果只包含了.config文件,则让
include .depend !CONFIGURATION=depend
else !如果没有包含.config文件,则让
CONFIGURATION = depend !CONFIGURATION=config
endif
else
CONFIGURATION = config
endif
ifdef CONFIGURATION !如果定义了CONFIGURATION,则让
CONFIGURE = dummy !CONFIGURATION=dummy,也就是说让.config endif !和.depend依赖于dummy
#
# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, CURRENT, /dev/xxxx or empty, in which case
# the default of FLOPPY is used by 'build'.
# ROOT_DEV是我们制作映像文件时缺省的根设备。它可以是FLOPPY,CURRENT,/dev/xxxx或者是空,另外,缺省的FLOPPY被用于“build”
ROOT_DEV = CURRENT
!根设备为CURRENT
#
# If you want to preset the SVGA mode, uncomment the next line and
# set SVGA_MODE to whatever number you want.
# Set it to -DSVGA_MODE=NORMAL_VGA if you just want the EGA/VGA mode.
# The number is the same as you would ordinarily press at bootup.
#
!如果你想调整SVGA模式,注释掉下行并且可以设置成你想要的任何模式,如果你想要
!EGV/VGA模式的话可以用设置-DSVGA_MODE=NORMAL_VGA,这个数字和你在启动
!时按下按键一样。
SVGA_MODE= -DSVGA_MODE=NORMAL_VGA
#
# standard CFLAGS
#
CFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe
!编译时用的选项
ifdef CONFIG_CPP !可以用C++编译器编译
CFLAGS := $(CFLAGS) -x c++
endif
ifdef CONFIG_M486 !处理器的定义,根据用户的选择可以让gcc为对应的
CFLAGS := $(CFLAGS) -m486 !CPU类型产生优化的代码
else
CFLAGS := $(CFLAGS) -m386
endif
#
# if you want the ram-disk device, define this to be the
# size in blocks.
# 如果你想使用虚拟盘,请定义这里的尺寸
#RAMDISK = -DRAMDISK=512
!定义虚拟盘的大小
AS86 =as86 -0 –a !8086汇编器
LD86 =ld86 -0 !8086连接器
AS =as !GNU汇编器
LD =ld !GNU连接器
HOSTCC =gcc !定义GCC
CC =gcc -D__KERNEL__ !-D__KERNEL__编译核心选项
MAKE =make
CPP =$(CC) -E
AR =ar
STRIP =strip
!从84行开始到106行,定义了最终要连接成在zImage时要用到的各个模块
ARCHIVES =kernel/kernel.o mm/mm.o fs/fs.o net/net.o ipc/ipc.o
FILESYSTEMS =fs/filesystems.a
DRIVERS =drivers/block/block.a /
drivers/char/char.a /
drivers/net/net.a /
ibcs/ibcs.o
LIBS =lib/lib.a
SUBDIRS =kernel drivers mm fs net ipc ibcs lib
KERNELHDRS =/usr/src/linux/include
ifdef CONFIG_SCSI
DRIVERS := $(DRIVERS) drivers/scsi/scsi.a
endif
ifdef CONFIG_SOUND
DRIVERS := $(DRIVERS) drivers/sound/sound.a
endif
ifdef CONFIG_MATH_EMULATION
DRIVERS := $(DRIVERS) drivers/FPU-emu/math.a
endif
.c.s:
$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
$(AS) -c -o $*.o $<
.c.o:
$(CC) $(CFLAGS) -c -o $*.o $<
Version: dummy
rm -f tools/version.h
config:
$(CONFIG_SHELL) Configure $(OPTS) < config.in
@if grep -s '^CONFIG_SOUND' .tmpconfig ; then /
$(MAKE) -C drivers/sound config; /
else : ; fi
mv .tmpconfig .config
linuxsubdirs: dummy !循环进入子目录汇编
set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done
tools/./version.h: tools/version.h !生成./version.h
tools/version.h: $(CONFIGURE) Makefile !生成tools/version.h
@./makever.sh
@echo /#define UTS_RELEASE /"$(VERSION).$(PATCHLEVEL)$(ALPHA)/" > tools/version.h
@echo /#define UTS_VERSION /"/#`cat .version` `date`/" >> tools/version.h
@echo /#define LINUX_COMPILE_TIME /"`date +%T`/" >> tools/version.h
@echo /#define LINUX_COMPILE_BY /"`whoami`/" >> tools/version.h
@echo /#define LINUX_COMPILE_HOST /"`hostname`/" >> tools/version.h
@echo /#define LINUX_COMPILE_DOMAIN /"`domainname`/" >> tools/version.h
tools/build: tools/build.c $(CONFIGURE) !生成tools/build
$(HOSTCC) $(CFLAGS) -o $@ $<
boot/head.o: $(CONFIGURE) boot/head.s !生成boot/head.o
boot/head.s: boot/head.S $(CONFIGURE) include/linux/tasks.h !生成boot/head.s
$(CPP) -traditional $< -o $@
tools/version.o: tools/version.c tools/version.h !生成tools/version.o
init/main.o: $(CONFIGURE) init/main.c !生成init/main.o
$(CC) $(CFLAGS) $(PROFILING) -c -o $*.o $<
tools/system: boot/head.o init/main.o tools/version.o linuxsubdirs
!在1.0核心中tools/system没有被使用,而是用了下面的tools/zSystem,所以从该行开始到160
!行我们都可以不用关心
$(LD) $(LDFLAGS) -Ttext 1000 boot/head.o init/main.o tools/version.o /
$(ARCHIVES) /
$(FILESYSTEMS) /
$(DRIVERS) /
$(LIBS) /
-o tools/system
nm tools/zSystem | grep -v '/(compiled/)/|/(/.o$$/)/|/( a /)' | /
sort > System.map
boot/setup: boot/setup.o !生成boot/setup
$(LD86) -s -o $@ $<
boot/setup.o: boot/setup.s !生成boot/setup.o
$(AS86) -o $@ $<
boot/setup.s: boot/setup.S $(CONFIGURE) include/linux/config.h Makefile
!生成boot/setup.s
$(CPP) -traditional $(SVGA_MODE) $(RAMDISK) $< -o $@
boot/bootsect: boot/bootsect.o !生成boot/bootsect
$(LD86) -s -o $@ $<
boot/bootsect.o: boot/bootsect.s !生成boot/bootsect.o
$(AS86) -o $@ $<
boot/bootsect.s: boot/bootsect.S $(CONFIGURE) include/linux/config.h Makefile
!生成boot/bootsect.s
$(CPP) -traditional $(SVGA_MODE) $(RAMDISK) $< -o $@
zBoot/zSystem: zBoot/*.c zBoot/*.S tools/zSystem
!z生成zBboot/zSystem
$(MAKE) -C zBoot
!生成在zImage,zImage=boot/bootsect+boot/setup+zBoot/zSystem (当然zImage并不是简单的由这3个模块的叠加,而是由tools/build这个应用程序去除了MINIX头和GCC头后才叠加的,最终生成zImage
zImage: $(CONFIGURE) boot/bootsect boot/setup zBoot/zSystem tools/build
tools/build boot/bootsect boot/setup zBoot/zSystem $(ROOT_DEV) > zImage
sync
zdisk: zImage !将生成的zImage写入软盘
dd bs=8192 if=zImage of=/dev/fd0
zlilo: $(CONFIGURE) zImage
!使用lilo启动核心,首先备份 ,系统原有的vmlinuz和zSystem.map,然后把生成的zImage和zSystem.map拷贝到”/“下,最后运行lilo,安装lilo
if [ -f /vmlinuz ]; then mv /vmlinuz /vmlinuz.old; fi
if [ -f /zSystem.map ]; then mv /zSystem.map /zSystem.old; fi
cat zImage > /vmlinuz
cp zSystem.map /
if [ -x /sbin/lilo ]; then /sbin/lilo; else /etc/lilo/install; fi
!生成tools/zSystem,tools/zSystem=boot/head.o+init/main.o+tools/version.o+各个编译好的子目录下的模块,请注意生成的tools/zSystem模块代码段从 1M 处开始编制
tools/zSystem: boot/head.o init/main.o tools/version.o linuxsubdirs
$(LD) $(LDFLAGS) -Ttext 100000 boot/head.o init/main.o tools/version.o /
$(ARCHIVES) /
$(FILESYSTEMS) /
$(DRIVERS) /
$(LIBS) /
-o tools/zSystem
nm tools/zSystem | grep -v '/(compiled/)/|/(/.o$$/)/|/( a /)' | /
sort > zSystem.map
!从这开始到226行,使编译各个子目录下的模块的情况
fs: dummy
$(MAKE) linuxsubdirs SUBDIRS=fs
lib: dummy
$(MAKE) linuxsubdirs SUBDIRS=lib
mm: dummy
$(MAKE) linuxsubdirs SUBDIRS=mm
ipc: dummy
$(MAKE) linuxsubdirs SUBDIRS=ipc
kernel: dummy
$(MAKE) linuxsubdirs SUBDIRS=kernel
drivers: dummy
$(MAKE) linuxsubdirs SUBDIRS=drivers
net: dummy
$(MAKE) linuxsubdirs SUBDIRS=net
!清除编译生成的目标文件
clean:
rm -f kernel/ksyms.lst
rm -f core `find . -name '*.[oas]' -print`
rm -f core `find . -name 'core' -print`
rm -f zImage zSystem.map tools/zSystem tools/system
rm -f Image System.map boot/bootsect boot/setup
rm -f zBoot/zSystem zBoot/xtract zBoot/piggyback
rm -f .tmp* drivers/sound/configure
rm -f init/*.o tools/build boot/*.o tools/*.o
!清除编译生成的目标文件,还包括生成的h文件和.config和.depend文件
mrproper: clean
rm -f include/linux/autoconf.h tools/version.h
rm -f drivers/sound/local.h
rm -f .version .config* config.old
rm -f .depend `find . -name .depend -print`
distclean: mrproper
!备份核心源代码
backup: mrproper
cd .. && tar cf - linux | gzip -9 > backup.gz
sync
!产生.depend文件
depend dep:
touch tools/version.h
for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done > .tmpdepend
for i in tools/*.c;do echo -n "tools/";$(CPP) -M $$i;done >> .tmpdepend
set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done
rm -f tools/version.h
mv .tmpdepend .depend
ifdef CONFIGURATION
!如果定义了CONFIGURATION,则说明上面的make config或者make depend来生成.config!和.depend文件,只有这两个文件都有了,才可以编译出核心来,最后让你重新make
..$(CONFIGURATION):
@echo
@echo "You have a bad or nonexistent" .$(CONFIGURATION) ": running 'make" $(CONFIGURATION)"'"
@echo
$(MAKE) $(CONFIGURATION)
@echo
@echo "Successful. Try re-making (ignore the error that follows)"
@echo
exit 1
dummy: ..$(CONFIGURATION)
else
dummy:
endif
#
# Leave these dummy entries for now to tell people that they are going away..
#
lilo:
@echo
@echo Uncompressed kernel images no longer supported. Use
@echo /"make zlilo/" instead.
@echo
@exit 1
Image:
@echo
@echo Uncompressed kernel images no longer supported. Use
@echo /"make zImage/" instead.
@echo
@exit 1
disk:
@echo
@echo Uncompressed kernel images no longer supported. Use
@echo /"make zdisk/" instead.
@echo
@exit 1
zBoot/Makefile:
HEAD = head.o
SYSTEM = ../tools/zSystem
#LD = gcc
#TEST = -DTEST_DRIVER
zOBJECTS = $(HEAD) inflate.o unzip.o misc.o
CFLAGS = -O2 -DSTDC_HEADERS $(TEST)
.c.s:
$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
$(AS) -c -o $*.o $<
.c.o:
$(CC) $(CFLAGS) -c -o $*.o $<
all: zSystem
!生成zSystem文件,!zSystem=$(Zobjects)+piggy.o=head.o+inflate.o+unzip.o+misc.o+piggy.o,zSystem代码段被从4k地址处开始编址连接,请注意piggy.o是真正的核心,并且已经以gzip -9压缩
zSystem: piggy.o $(zOBJECTS)
$(LD) $(LDFLAGS) -o zSystem -Ttext 1000 $(zOBJECTS) piggy.o
head.o: head.s
head.s: head.S ../include/linux/tasks.h
$(CPP) -traditional head.S -o head.s
piggy.o: $(SYSTEM) xtract piggyback
./xtract $(SYSTEM) | gzip -9 | ./piggyback > piggy.o
$(SYSTEM):
$(MAKE) -C .. tools/zSystem
Inflate.c,unzip.c,misc.c中定义的函数,这些函数用于解压缩被用gzip -9压缩的核心,并且把解压缩的核心从物理地址 1M 开始处放置。