macos m1下编译linux内核

说明

结合网上一些资源并折腾了一段时间后才成功的。因为完全参考无法正常编译,记录一下供有需要的,希望能对其有所帮助

uboot也可以参考编译通过

macos版本

uname -a
Darwin finnmac-mini.local 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101 arm64

安装环境

brew install make
brew install aarch64-elf-gcc
brew install openssl@1.1

依赖环境

elf.h

个人在内核源码同级新建include目录,拷贝其中

编译步骤

/opt/homebrew/opt/make/libexec/gnubin/make ARCH=arm64 CROSS_COMPILE=aarch64-elf- defconfig

# 去掉CONFIG_KVM选项避免不必要的报错
# [ ] Virtualization  ----
/opt/homebrew/opt/make/libexec/gnubin/make ARCH=arm64 CROSS_COMPILE=aarch64-elf- menuconfig
  • 修改内核
diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 1c451d5..0d66689 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -43,6 +43,10 @@ typedef struct {
        __u8 b[16];
 } uuid_le;

+#ifdef __APPLE__
+#define uuid_t compat_uuid_t
+#endif
+
 typedef struct {
        __u8 b[16];
 } uuid_t;
  • 编译内核
/opt/homebrew/opt/make/libexec/gnubin/make ARCH=arm64 CROSS_COMPILE=aarch64-elf- HOSTCFLAGS="-I../include -I/opt/homebrew/opt/openssl@1.1/include/" HOSTLDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib/" -j8

效果展示

(master) ~/WorkSpace/github/linux-5.18.9 ✘ ls
1.txt                   Kconfig                 README                  compile_commands.json   init                    modules-only.symvers    samples                 usr
COPYING                 LICENSES                System.map              crypto                  ipc                     modules.builtin         scripts                 virt
CREDITS                 MAINTAINERS             arch                    drivers                 kernel                  modules.builtin.modinfo security                vmlinux
Documentation           Makefile                block                   fs                      lib                     modules.order           sound                   vmlinux.o
Kbuild                  Module.symvers          certs                   include                 mm                      net                     tools                   vmlinux.symvers
(master) ~/WorkSpace/github/linux-5.18.9 ✘ file vmlinux
vmlinux: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), statically linked, BuildID[sha1]=3cfb7eb4d00450fdddde0bf6f1c1dcef63b7ad4a, not stripped
(master) ~/WorkSpace/github/linux-5.18.9 ✘
posted @ 2022-07-11 03:12  rdwr  阅读(2946)  评论(0编辑  收藏  举报