定製編譯內核模塊的GCC參數

下面的補丁,在編譯內核模塊時,編譯每個C文件後,同時輸出一個".E"後綴的預處理文件

--- linux-3.7.orig/scripts/Makefile.build    2012-12-11 11:30:57.000000000 +0800
+++ linux-3.7/scripts/Makefile.build    2013-03-28 16:37:34.000000000 +0800
@@ -232,6 +232,7 @@

ifndef CONFIG_MODVERSIONS
cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
+hack_cmd_cc_o_c = $(CC) $(c_flags) -E -o $<.E $<

else
# When module versioning is enabled the following steps are executed:
@@ -293,6 +294,7 @@
 define rule_cc_o_c
     $(call echo-cmd,checksrc) $(cmd_checksrc)              \
     $(call echo-cmd,cc_o_c) $(cmd_cc_o_c);                  \
+    $(hack_cmd_cc_o_c);                          \
     $(cmd_modversions)                          \
     $(call echo-cmd,record_mcount)                      \
     $(cmd_record_mcount)                          \
運行的情況如下:
# cat test.c
#include <linux/module.h>
#include <linux/preempt.h>

int test_init(void)
{
  int i;

  i = preempt_count();
  barrier();
  printk ("%d\n", i);
  return i;
}
module_init(test_init);

void test_exit(void)
{
}
module_exit(test_exit);

MODULE_LICENSE("GPL");
# cat Makefile
obj-m += test.o

KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build/
PWD := $(shell pwd)

all:
    make -C $(KDIR) M=$(PWD) modules

clean:
    make -C $(KDIR) M=$(PWD) clean
    rm -f *~ *.E
# make
make -C /lib/modules/3.7.0/build/ M=/root/git/driver modules
make[1]: Entering directory `/root/source/linux/linux-3.7'
  CC [M]  /root/git/driver/test.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/git/driver/test.mod.o
  LD [M]  /root/git/driver/test.ko
make[1]: Leaving directory `/root/source/linux/linux-3.7'
# cat test.c.E
int test_init(void)
{
  int i;

  i = (current_thread_info()->preempt_count);
  __asm__ __volatile__("": : :"memory");
  printk ("%d\n", i);
  return i;
}
#
ifndef CONFIG_MODVERSIONS
cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
+hack_cmd_cc_o_c = $(CC) $(c_flags) -E -o $<.E $<

else
# When module versioning is enabled the following steps are executed:
# 有時會必須加在else
+hack_cmd_cc_o_c = $(CC) $(c_flags) -E -o $<.E $<

@@ -293,6 +294,7 @@
define rule_cc_o_c
     $(call echo-cmd,checksrc) $(cmd_checksrc)              \
     $(call echo-cmd,cc_o_c) $(cmd_cc_o_c);                  \
+    $(hack_cmd_cc_o_c);                          \
     $(cmd_modversions)                          \
     $(call echo-cmd,record_mcount)                      \
     $(cmd_record_mcount)                          \
運行的情況如下:

a


书籍推荐