【问题标题】:Can't build hello world kernel module on Android JellyBean无法在 Android JellyBean 上构建 hello world 内核模块
【发布时间】:2013-01-17 22:29:58
【问题描述】:

我正在尝试在 Android JellyBean 上构建一个简单的内核模块。

代码:

#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>  /* Needed for KERN_ALERT */

MODULE_LICENSE("GPL");
MODULE_AUTHOR("test");
MODULE_DESCRIPTION("Android ko test");

int init_module(void)
{
   printk(KERN_ALERT, "Hello world\n");

   // A non 0 return means init_module failed; module can't be loaded.
   return 0;
}

void cleanup_module(void)
{
  printk(KERN_ALERT "Goodbye world 1.\n");
}

生成文件:

obj-m +=hello.o

KERNELDIR ?= ~/android/kernel
PWD := $(shell pwd)
CROSS_COMPILE=~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
ARCH=arm

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules

clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

输出:

make -C ~/android/kernel M=/home/test/testmod ARCH=arm CROSS_COMPILE=~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- modules
make[1]: Entering directory `/home/test/android/kernel'

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /home/test/android/kernel/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /home/test/testmod/hello.o
In file included from <command-line>:0:
/home/test/android/kernel/include/linux/kconfig.h:4:32: error: generated/autoconf.h: No such file or directory
In file included from /home/test/android/kernel/arch/arm/include/asm/types.h:4,
                 from include/linux/types.h:4,
                 from include/linux/list.h:4,
                 from include/linux/module.h:9,
                 from /home/test/testmod/hello.c:1:
include/asm-generic/int-ll64.h:11:29: error: asm/bitsperlong.h: No such file or directory
In file included from /home/test/android/kernel/arch/arm/include/asm/posix_types.h:38,
                 from include/linux/posix_types.h:47,
                 from include/linux/types.h:17,
                 from include/linux/list.h:4,
                 from include/linux/module.h:9,
                 from /home/test/testmod/hello.c:1:
include/asm-generic/posix_types.h:70:5: warning: "__BITS_PER_LONG" is not defined
error, forbidden warning: posix_types.h:70
make[2]: *** [/home/test/testmod/hello.o] Error 1
make[1]: *** [_module_/home/test/testmod] Error 2
make[1]: Leaving directory `/home/test/android/kernel'
make: *** [default] Error 2

如果我按照输出中的建议,在内核上运行“make oldconfig && make prepare”,它会引导我完成几十个内核配置是/否问题。之后,编译仍然失败,下一个错误是关于 bitsperlong.h。

【问题讨论】:

    标签: android linux android-4.2-jelly-bean


    【解决方案1】:

    Android 将输出二进制文件放在 out 目录下。例如,如果定义了$ANDROID_PRODUCT_OUT,则可以使用out/target/product/&lt;target name&gt;/obj/KERNEL_OBJ/$ANDROID_PRODUCT_OUT/obj/KERNEL_OBJ/。此目录可能与不同供应商的名称不同,但就是包含vmlinux 的目录。

    所以当你在 Android repo 下编译内核模块时,你应该在你的模块目录中提交make 命令,如下所示。

    make -C $ANDROID_PRODUCT_OUT/obj/KERNEL_OBJ/ M=`pwd` ARCH=arm CROSS_COMPILE=arm-eabi- modules
    

    【讨论】:

      【解决方案2】:

      在编译内核至少一次之后,应完成制作模块。您尚未编译内核,这就是 Module.symvers 缺失的原因。在编译过程中会创建一些像 asm/bitsperlong.h 这样的头文件。

      【讨论】:

      • 这是我在 Yocto 生成的 SDK 中构建树外模块时遇到的问题。 $SYSROOT/usr/src/kernel/arch/arm/include 中缺少 $KERNEL_DIR/build/arch/arm/include/generated 目录,必须手动复制。
      【解决方案3】:

      首先确保你已经在指定路径编译内核。 那就是

      " /home/test/android/kernel" but you are using 
      " /home/android/kernel "     during compilation of module
      KERNELDIR ?= ~/android/kernel has to be KERNELDIR ?= ~/test/android/kernel
      

      如果没有,则在~/android/kernel 目录中运行以下命令编译内核。

      make ARCH=arm CROSS_COMPILE=~/test/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi
      

      编译内核后,您将在内核 i,e 的 System.map 文件中定义此“__BITS_PER_LONG”变量

      ~/test/android/kernel/System.map
      

      在此之后,您将能够毫无障碍地编译您的模块

      【讨论】:

      • 实际上,~/android/kernel 解析为 /home/test/android/kernel。您可以从 Makefile 输出中看到,其中显示 Entering directory `/home/test/android/kernel'。我尝试了显式路径而不是〜,但我得到了同样的错误。
      • 是的,内核已经编译好了。我也用它闪现了我的目标。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多