注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Tsecer的回音岛

Tsecer的博客

 
 
 

日志

 
 

如何构建一个不带优化的内核  

2017-08-18 22:40:49|  分类: Linux内核 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
一、构建编译失败
为了便于调试内核,想禁掉内核的优化选项,但是在编译的时候有报错,下面是在构建3.12内核的386版本时的编译错误:
  gcc -Wp,-MD,arch/x86/kernel/.process_32.o.d  -nostdinc -isystem /usr/lib/gcc/i686-redhat-linux/4.4.7/include -I/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include -Iarch/x86/include/generated  -Iinclude -I/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/tsecer/kerneldebug/linux-3.12.6/include/uapi -Iinclude/generated/uapi -include /home/tsecer/kerneldebug/linux-3.12.6/include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -m32 -msoft-float -mregparm=3 -freg-struct-return -mno-mmx -mno-sse -fno-pic -mpreferred-stack-boundary=2 -march=i686 -Wa,-mtune=generic32 -ffreestanding -fstack-protector -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_AVX=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -Wframe-larger-than=1024 -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -fno-inline-functions-called-once -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -DCC_HAVE_ASM_GOTO    -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(process_32)"  -D"KBUILD_MODNAME=KBUILD_STR(process_32)" -c -o arch/x86/kernel/.tmp_process_32.o arch/x86/kernel/process_32.c
In file included from /home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/alternative.h:132,
                 from /home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/bitops.h:16,
                 from include/linux/bitops.h:22,
                 from include/linux/kernel.h:10,
                 from include/linux/cache.h:4,
                 from include/linux/time.h:4,
                 from include/linux/stat.h:18,
                 from include/linux/sysfs.h:20,
                 from include/linux/kobject.h:21,
                 from include/linux/device.h:17,
                 from include/linux/node.h:17,
                 from include/linux/cpu.h:16,
                 from arch/x86/kernel/process_32.c:12:
/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/cpufeature.h: In function ‘fpu_save_init’:
/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/cpufeature.h:416: warning: asm operand 1 probably doesn’t match constraints
/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/cpufeature.h:416: warning: asm operand 1 probably doesn’t match constraints
In file included from arch/x86/kernel/process_32.c:44:
/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/cpufeature.h:416: error: impossible constraint in ‘asm’
/home/tsecer/kerneldebug/linux-3.12.6/arch/x86/include/asm/cpufeature.h:416: error: impossible constraint in ‘asm’
make[2]: *** [arch/x86/kernel/process_32.o] Error 1
make[1]: *** [arch/x86/kernel] Error 2

二、问题
这个问题的原因几乎不用看代码,应该就是由于禁掉优化导致的编译的问题,不过之前有印象内核可以为每个文件指明定制化的编译选项,所以可以对这些文件进行妥协,也就是对它们以最小幅度开启优化。

1、c文件的构建命令
linux-3.12.6\scripts\Makefile.build
cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<

2、c_flags的定义
linux-3.12.6\scripts\Makefile.lib
# These flags are needed for modversions and compiling, so we define them here
# already
# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will 
# end up in (or would, if it gets compiled in)
# Note: Files that end up in two or more modules are compiled without the
#       KBUILD_MODNAME definition. The reason is that any made-up name would
#       differ in different configs.
name-fix = $(subst $(comma),_,$(subst -,_,$1))
basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
modname_flags  = $(if $(filter 1,$(words $(modname))),\
                 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")

orig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
                 $(ccflags-y) $(CFLAGS_$(basetarget).o)
_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(orig_c_flags))
_a_flags       = $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(KBUILD_SUBDIR_ASFLAGS) \
                 $(asflags-y) $(AFLAGS_$(basetarget).o)
_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
……
c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
$(__c_flags) $(modkern_cflags)                           \
-D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)

3、basetarget的定义
linux-3.12.6\scripts\Kbuild.include
# filename of target with directory and extension stripped
basetarget = $(basename $(notdir $@))

三、执行的命令
所以对于开始的时候的编译错误文件 arch/x86/kernel/process_32.c ,订制的编译选项就是CFLAGS_process_32.o,整理之后的编译命令为:
make CFLAGS_process_32.o=-O1 V=1 CFLAGS_signal.o=-O1 CFLAGS_traps.o=-O1 CFLAGS_process.o=-O1 CFLAGS_i387.o=-O1 CFLAGS_xsave.o=-O1 CFLAGS_proc.o=-O1 CFLAGS_fault.o=-O1 CFLAGS_memory.o=-O1 CFLAGS_mprotect.o=-O1 CFLAGS_fault.o=-O1 CFLAGS_mremap.o=-O1 CFLAGS_task_mmu.o=-O1 CFLAGS_powernow-k8.o=-O1

  评论这张
 
阅读(37)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017