Skip to content

Commit f06333e

Browse files
WIP: build Linux on MacOS
I just built the ARCH=arm64 defconfig Linux kernel on MacOS Monterey 12.5.1! $ make LLVM=1 -j8 Hacked up patch for now, will need to clean these up and break them up into individual patches before sending. Issues Encountered: 1. SUBARCH defaults to arm rather than arm64; fix regex in scripts/subarch.include. 2. MacOS doesn't distribute elf.h and neither does Homebrew. I used gelf.h from libelf from Homebrew, but then had to muck up a bunch of build flags to invoke pkg-config via HOSTPKG_CONFIG. I think a better approach would be just to include a minimal elf.h in the kernel sources for MacOS hosts, similar to https://gist.github.com/mlafeldt/3885346 but smaller. 3. MacOS doesn't distribute endian.h. I used a combination of machine/endian.h and https://gist.github.com/yinyin/2027912, but probably should include a minimal copy in kernel sources similar to elf.h above. 4. pkg-config doesn't detect libcrypto from openssl from HomeBrew because MacOS uses LibreSSL or something. brew install openssl prints a warning about it. 5. MacOS' FORTIFY_SOURCE implementation seems broken wrt. scripts/mod/file2alias.c. 6. MacOS sys/types.h defines uuid_t, which conflicts with scripts/mod/file2alias.c. For the build, I built the following from source: 1. clang 2. lld 3. llvm-nm 4. llvm-objcopy 5. llvm-objdump 6. llvm-strip From Homebrew I needed to install: 1. libelf (probably not necessary) 2. pkg-config 3. bash (the one that ships with macos is too old, doesn't understand lastpipe) 4. md5sha1sum 5. openssl Before all of the above I needed to create a case-sensitive volume to check out the kernel sources. It probably wouldn't hurt to add the above to some documentation. Signed-off-by: Nick Desaulniers <nick.desaulniers@gmail.com>
1 parent f76349c commit f06333e

17 files changed

Lines changed: 40 additions & 9 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ include $(srctree)/scripts/subarch.include
385385
# Default value for CROSS_COMPILE is not to prefix executables
386386
# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
387387
ARCH ?= $(SUBARCH)
388+
$(info $$SUBARCH is [${SUBARCH}])
388389

389390
# Architecture as present in compile.h
390391
UTS_MACHINE := $(ARCH)

arch/arm/vdso/vdsomunge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* it does.
3333
*/
3434

35-
#include <elf.h>
35+
#include <gelf.h>
3636
#include <errno.h>
3737
#include <fcntl.h>
3838
#include <stdarg.h>

arch/arm64/kernel/vdso32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ VDSO_LDFLAGS += --orphan-handling=warn
111111
# We have to use a relative path because scripts/Makefile.host prefixes
112112
# $(hostprogs) with $(obj)
113113
munge := ../../../arm/vdso/vdsomunge
114+
HOST_EXTRACFLAGS += $(shell $(HOSTPKG_CONFIG) --cflags libelf) -include $(obj)/elf_helper.h
114115
hostprogs := $(munge)
115116

116117
c-obj-vdso := note.o
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define EF_ARM_EABIMASK 0XFF000000
2+
#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)

arch/arm64/kvm/hyp/nvhe/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMI
1313

1414
hostprogs := gen-hyprel
1515
HOST_EXTRACFLAGS += -I$(objtree)/include
16+
HOST_EXTRACFLAGS += $(shell $(HOSTPKG_CONFIG) --cflags libelf)
17+
HOST_EXTRACFLAGS += -include $(obj)/endian_helper.h
1618

1719
lib-objs := clear_page.o copy_page.o memcpy.o memset.o
1820
lib-objs := $(addprefix ../../../lib/, $(lib-objs))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <libkern/OSByteOrder.h>
2+
#define le16toh(x) OSSwapLittleToHostInt16(x)
3+
#define le32toh(x) OSSwapLittleToHostInt32(x)
4+
#define le64toh(x) OSSwapLittleToHostInt64(x)
5+
#define EM_AARCH64 183

arch/arm64/kvm/hyp/nvhe/gen-hyprel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* However, those are only generated if CONFIG_RELOCATABLE=y.
2525
*/
2626

27-
#include <elf.h>
28-
#include <endian.h>
27+
#include <gelf.h>
28+
#include <machine/endian.h>
2929
#include <errno.h>
3030
#include <fcntl.h>
3131
#include <stdbool.h>

certs/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ targets += x509_revocation_list
8585
hostprogs := extract-cert
8686

8787
HOSTCFLAGS_extract-cert.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null)
88+
HOSTCFLAGS_extract-cert.o += -I/opt/homebrew/opt/openssl@3/include
89+
HOSTCFLAGS_extract-cert.o += -L/opt/homebrew/opt/openssl@3/lib
8890
HOSTLDLIBS_extract-cert = $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)

scripts/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
1111
hostprogs-always-$(CONFIG_MODULE_SIG_FORMAT) += sign-file
1212
hostprogs-always-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert
1313

14-
HOSTCFLAGS_sorttable.o = -I$(srctree)/tools/include
14+
HOSTCFLAGS_sorttable.o = -I$(srctree)/tools/include $(shell $(HOSTPKG_CONFIG) --cflags libelf)
1515
HOSTLDLIBS_sorttable = -lpthread
1616
HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
1717
HOSTCFLAGS_sign-file.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null)

scripts/mod/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ OBJECT_FILES_NON_STANDARD := y
33
CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO)
44

55
hostprogs-always-y += modpost mk_elfconfig
6+
libelf_flags := $(shell $(HOSTPKG_CONFIG) --cflags libelf)
7+
HOSTCFLAGS_mk_elfconfig.o += $(libelf_flags)
8+
HOSTCFLAGS_modpost.o += $(libelf_flags) -include $(obj)/elf_helper.h
9+
HOSTCFLAGS_sumversion.o += $(libelf_flags)
10+
#HOSTCFLAGS_file2alias.o += $(libelf_flags) -U_FORTIFY_SOURCE
11+
HOSTCFLAGS_file2alias.o += $(libelf_flags) -D_FORTIFY_SOURCE=0
612
always-y += empty.o
713

814
modpost-objs := modpost.o file2alias.o sumversion.o

0 commit comments

Comments
 (0)