linux kernel编译产生的vmlinux Image zImage之间的关系

linux kernel的编译过程比较复杂,最后生成的给用户使用的kernel文件也是多种多样。
先来看一段编译ARM kernel时命令行最后的输出,主要可以分为5个步骤:
- 生成vmlinux
- 生成arch/arm/boot/Image
- 生成arch/arm/boot/compressed/piggy.gzip
- 生成arch/arm/boot/compressed/vmlinux
- 生成arch/arm/boot/zImage

  LD      vmlinux
  SYSMAP  System.map
  OBJCOPY arch/arm/boot/Image
  Kernel: arch/arm/boot/Image is ready
  Building modules, stage 2.
  GZIP    arch/arm/boot/compressed/piggy.gzip
  MODPOST 1 modules
  CC      arch/arm/boot/compressed/misc.o
  CC      drivers/scsi/scsi_wait_scan.mod.o
  CC      arch/arm/boot/compressed/decompress.o
  CC      arch/arm/boot/compressed/string.o
  LD [M]  drivers/scsi/scsi_wait_scan.ko
  AS      arch/arm/boot/compressed/piggy.gzip.o
  LD      arch/arm/boot/compressed/vmlinux
  OBJCOPY arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready

我们来逐步分析每一个步骤:

生成vmlinux

vmlinux生成在kernel的根目录下,大小为28MB。链接文件在arch/arm/kernel/vmlinux.lds。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ ll
......
-rwxrwxr-x   1 panzhenjie panzhenjie 28289095  91 07:27 vmlinux*

这个vmlinux是一个elf格式的文件,通过readelf命令我们可以看到,这个文件的链接地址是0xc0008000。
从第1段.head.text开始到第22段.bss,一共占用了2MB多的空间,而后面20多MB的数据其实都是些不必要的调试信息。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ readelf -S vmlinux
There are 36 section headers, starting at offset 0x1a09770:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .head.text        PROGBITS        c0008000 008000 0001c4 00  AX  0   0  4
  [ 2] .text             PROGBITS        c0008200 008200 1948e4 00  AX  0   0 64
  [ 3] .rodata           PROGBITS        c019d000 19d000 072100 00   A  0   0 64
  [ 4] __bug_table       PROGBITS        c020f100 20f100 0028c8 00   A  0   0  1
  [ 5] __ksymtab         PROGBITS        c02119c8 2119c8 003c28 00   A  0   0  4
  [ 6] __ksymtab_gpl     PROGBITS        c02155f0 2155f0 001988 00   A  0   0  4
  [ 7] __ksymtab_strings PROGBITS        c0216f78 216f78 00b9b8 00   A  0   0  1
  [ 8] __param           PROGBITS        c0222930 222930 000340 00   A  0   0  4
  [ 9] __modver          PROGBITS        c0222c70 222c70 000390 00   A  0   0  4
  [10] .ARM.unwind_idx   ARM_EXIDX       c0223000 223000 00ea60 00  AL 12   0  4
  [11] .ARM.unwind_tab   PROGBITS        c0231a60 231a60 002964 00   A  0   0  4
  [12] .init.text        PROGBITS        c0235000 235000 0182f8 00  AX  0   0 32
  [13] .exit.text        PROGBITS        c024d2f8 24d2f8 000480 00  AX  0   0  4
  [14] .init.arch.info   PROGBITS        c024d778 24d778 000240 00   A  0   0  4
  [15] .init.tagtable    PROGBITS        c024d9b8 24d9b8 000048 00   A  0   0  4
  [16] .init.smpalt      PROGBITS        c024da00 24da00 0001d8 00   A  0   0  4
  [17] .init.pv_table    PROGBITS        c024dbd8 24dbd8 000488 00   A  0   0  1
  [18] .init.data        PROGBITS        c024e060 24e060 0081ac 00  WA  0   0  8
  [19] .data..percpu     PROGBITS        c0257000 257000 001480 00  WA  0   0 64
  [20] .data             PROGBITS        c025a000 25a000 042760 00  WA  0   0 64
  [21] .notes            NOTE            c029c760 29c760 000024 00  AX  0   0  4
  [22] .bss              NOBITS          c029c7c0 29c784 0319ec 00  WA  0   0 64
  [23] .comment          PROGBITS        00000000 29c784 00002b 01  MS  0   0  1
  [24] .ARM.attributes   ARM_ATTRIBUTES  00000000 29c7af 00002d 00      0   0  1
  [25] .debug_line       PROGBITS        00000000 29c7dc 140a16 00      0   0  1
  [26] .debug_info       PROGBITS        00000000 3dd1f2 10da832 00      0   0  1
  [27] .debug_abbrev     PROGBITS        00000000 14b7a24 0b90ba 00      0   0  1
  [28] .debug_aranges    PROGBITS        00000000 1570ae0 006910 00      0   0  8
  [29] .debug_ranges     PROGBITS        00000000 15773f0 0a11e0 00      0   0  8
  [30] .debug_frame      PROGBITS        00000000 16185d0 03ef0c 00      0   0  4
  [31] .debug_str        PROGBITS        00000000 16574dc 088dbd 01  MS  0   0  1
  [32] .debug_loc        PROGBITS        00000000 16e0299 32934c 00      0   0  1
  [33] .shstrtab         STRTAB          00000000 1a095e5 00018a 00      0   0  1
  [34] .symtab           SYMTAB          00000000 1a09d10 0908d0 10     35 30957  4
  [35] .strtab           STRTAB          00000000 1a9a5e0 060267 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

生成arch/arm/boot/Image
Image文件被除去了一些不必要的调试信息,是一个已经可以使用了的kernel文件了,只有2.7MB。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ ll arch/arm/boot/
......
-rwxrwxr-x  1 panzhenjie panzhenjie 2705284  91 07:27 Image*
arch/arm/boot/Makefile

$(obj)/Image: vmlinux FORCE
    $(call if_changed,objcopy)
    @echo '  Kernel: $@ is ready'
scripts/Kbuild.include

# Execute command if command has changed or prerequisite(s) are updated.
#
if_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
    @set -e;                                                             \
    $(echo-cmd) $(cmd_$(1));                                             \
    echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
scripts/Makefile.lib

# Objcopy
# ---------------------------------------------------------------------------

quiet_cmd_objcopy = OBJCOPY $@
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
arch/arm/Makefile

OBJCOPYFLAGS    :=-O binary -R .comment -S

生成arch/arm/boot/compressed/piggy.gzip
为了让kernel更小,下一步就是对kernel进行压缩,压缩出来的kernel变成了1.3MB。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ ll arch/arm/boot/compressed/
......
-rw-rw-r-- 1 panzhenjie panzhenjie 1311566  91 07:27 piggy.gzip
arch/arm/boot/compressed/Makefile

suffix_$(CONFIG_KERNEL_GZIP) = gzip
suffix_$(CONFIG_KERNEL_LZO)  = lzo
suffix_$(CONFIG_KERNEL_LZMA) = lzma
suffix_$(CONFIG_KERNEL_XZ)   = xzkern

$(obj)/piggy.$(suffix_y): $(obj)/../Image FORCE
    $(call if_changed,$(suffix_y))
scripts/Makefile.lib

# Gzip
# ---------------------------------------------------------------------------

quiet_cmd_gzip = GZIP    $@
cmd_gzip = (cat $(filter-out FORCE,$^) | gzip -n -f -9 > $@) || \
    (rm -f $@ ; false)

生成arch/arm/boot/compressed/vmlinux
piggy.zip只是对kernel进行了压缩,但是还并不能让我们直接使用。想要使用,必须要加上自解压缩代码。
可以看到,生成出来的elf格式的kernel就比刚才压缩过的kernel大一点而已。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ ll arch/arm/boot/compressed/
......
-rwxrwxr-x 1 panzhenjie panzhenjie 1404652  91 07:27 vmlinux*
arch/arm/boot/compressed/Makefile

$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
        $(addprefix $(obj)/, $(OBJS)) $(lib1funcs) $(ashldi3) FORCE
    @$(check_for_multiple_zreladdr)
    $(call if_changed,ld)
    @$(check_for_bad_syms)

$(obj)/piggy.$(suffix_y).o:  $(obj)/piggy.$(suffix_y) FORCE

生成arch/arm/boot/zImage
最后一步,就是剔除掉新的压缩后的kernel中的调试信息,成为一个真正可以使用的带有自解压功能的kernel。

panzhenjie@panzhenjie-vmware:~/workspace/linux-3.5$ ll arch/arm/boot/
......
-rwxrwxr-x  1 panzhenjie panzhenjie 1329592  91 07:27 zImage*
arch/arm/boot/Makefile

$(obj)/zImage:	$(obj)/compressed/vmlinux FORCE
    $(call if_changed,objcopy)
    @echo '  Kernel: $@ is ready'

你可能感兴趣的:(学习)