From patchwork Sat Jul 27 07:42:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13743626 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20A0A40856; Sat, 27 Jul 2024 07:45:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066337; cv=none; b=SZ8IKKnx51tTmNfi9jqzZivcovcv5EW3OXfYeKyC7Tm9x+Gk7/zAS17G3HyxotVqe670QOaY9L6r/Sh3TUZXpGeAl4Oa6RzAaapII/I3TPdptBrck56+K0CtO/J5aYfnMNu9uPtac6N/NVyUS+Bw+UtQdOqZKNOHYZC5yhui/Ao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066337; c=relaxed/simple; bh=NDllRo2iGl1hfEBaSYSoTi2556pDny/MrzqazaQB+yg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fueu9Z1e8H6zcfzUbhgnzoN+A8lGJ35ufC/Ftatk4NkQduQpS0sy5QO4M+QvLGh5Z2WCmc8FKvnMmjpzu40CBTQWvPkL8NOcefqhtGZ+SP1XwJniQ4uQA+SNgh1GxDzT+ze5jzBMa7N9TS8iuAOTqQmnHj89xWJrYT7yCIbTEXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d2Ko0rMl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d2Ko0rMl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA456C4AF09; Sat, 27 Jul 2024 07:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722066336; bh=NDllRo2iGl1hfEBaSYSoTi2556pDny/MrzqazaQB+yg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d2Ko0rMlFEb57lywUf2G6bVlIa6NbkH/s88JL3qjGQ4zIXqzibvzszR6i00SjZIef ZWHaTrg0WG5AZ50dTICNwYgFosGVLdJXq+FejTPqLcuoQPDxVXGMNj3din3eGsYfZy 2A3Hq2IchoSPg+mwVF5o5ZrDm+gLq6bGFR8m1guz+j8Zl8QMeRz2eGkuOeWO6ensUw vLwwHHnhE9QVxnIQwktk9vvUQmzJcQN6DoLarUZxSgRf424HPnGssf1sshod953zPg 1pjb3rxVHnOzYPd2mZrrSZjOXqvEtNKVix6K5N+1bS0+DAwjDnRJ38NmmF6LTgOrMI V06jdgxwzL1zw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Kees Cook , Nathan Chancellor , Nicolas Schier , Ben Hutchings , Masahiro Yamada Subject: [PATCH 1/4] modpost: remove unused HOST_ELFCLASS Date: Sat, 27 Jul 2024 16:42:01 +0900 Message-ID: <20240727074526.1771247-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240727074526.1771247-1-masahiroy@kernel.org> References: <20240727074526.1771247-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 HOST_ELFCLASS is output to elfconfig.h, but it is not used in modpost. Signed-off-by: Masahiro Yamada Reviewed-by: Nicolas Schier --- scripts/mod/mk_elfconfig.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/scripts/mod/mk_elfconfig.c b/scripts/mod/mk_elfconfig.c index 680eade89be1..aca96b3aada0 100644 --- a/scripts/mod/mk_elfconfig.c +++ b/scripts/mod/mk_elfconfig.c @@ -39,12 +39,6 @@ main(int argc, char **argv) exit(1); } - if (sizeof(unsigned long) == 4) { - printf("#define HOST_ELFCLASS ELFCLASS32\n"); - } else if (sizeof(unsigned long) == 8) { - printf("#define HOST_ELFCLASS ELFCLASS64\n"); - } - endian_test.s = 0x0102; if (memcmp(endian_test.c, "\x01\x02", 2) == 0) printf("#define HOST_ELFDATA ELFDATA2MSB\n"); From patchwork Sat Jul 27 07:42:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13743627 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0EDD433DC; Sat, 27 Jul 2024 07:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066339; cv=none; b=T+v2r/pnPGvNYiJgaSiDUPNOVq+wCEdA0e3dl83FtDBJBn9WauFdxnycJxsgGGZ9Zv1SH+HPUHVVDulNzdJiisdPiY/2BQomx2uG3sdGZNLBOd3XX2qtjS9m8bxkM99a4QUnd1Imrhgta+rlCPo++65fqGrj1Bcg+tF85usimQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066339; c=relaxed/simple; bh=odjYNOw6Q3VhuywTHHYXP+Ya9uttebtWitzebGDplN4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=luv7hvMmP901bSIfUY/2vrpoBy4EG+sRDQfgPgAy2/7PmDtHxI9UTRNOuDycHs9x6qIqDtEaHS5E5dtIaxzgbcjbQBK0dhpPs1TCk7G6Po0+VZ4WuqA2Gvks9FE0gxK8P+scqwruH/rswCiJQf+bGQNSxily2b+0ndN8mFIXcw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Dyc/8bgD; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Dyc/8bgD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E8B7C4AF0F; Sat, 27 Jul 2024 07:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722066338; bh=odjYNOw6Q3VhuywTHHYXP+Ya9uttebtWitzebGDplN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dyc/8bgDWepu5bXA4FSBDz2QXfyWAXRZpExXsuKNGzuCt5oZV3yn5qZzvfDEJfQ38 fhXibfgP631hQ/tyk3yoG7hRehHlQur4/AB/NKyYUNTHIxEg6s06RQROY2dka8nHaX O5nu6UhY5dH/4pAP6JR7FUAymVOW/qtLe9O9VqPYkoA/yslmRIMhSnvDL4SHVjka05 KPJfKjHp/9rJAS/pReewI6qAmW91WZnJzJOPZS2LkGYvgIkXVON+acNb9Mqjas8cg4 Ks300bZXAI9iknXrlGazSgZXbFfisUst5dXsmFk8EME8Df7D02I74M4KFWnGCScUeA 2+RdXFMf6PoHQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Kees Cook , Nathan Chancellor , Nicolas Schier , Ben Hutchings , Masahiro Yamada Subject: [PATCH 2/4] modpost: detect endianness on run-time Date: Sat, 27 Jul 2024 16:42:02 +0900 Message-ID: <20240727074526.1771247-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240727074526.1771247-1-masahiroy@kernel.org> References: <20240727074526.1771247-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Endianness is currently detected on compile-time, but we can defer this until run-time. This change avoids re-executing scripts/mod/mk_elfconfig even if modpost in the linux-headers package needs to be rebuilt for a foreign architecture. Signed-off-by: Masahiro Yamada Reviewed-by: Nicolas Schier --- scripts/mod/mk_elfconfig.c | 19 ------------------- scripts/mod/modpost.c | 36 ++++++++++++++++++++++++++++++++++++ scripts/mod/modpost.h | 13 ++++--------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/scripts/mod/mk_elfconfig.c b/scripts/mod/mk_elfconfig.c index aca96b3aada0..e8cee4e4bc73 100644 --- a/scripts/mod/mk_elfconfig.c +++ b/scripts/mod/mk_elfconfig.c @@ -8,7 +8,6 @@ int main(int argc, char **argv) { unsigned char ei[EI_NIDENT]; - union { short s; char c[2]; } endian_test; if (fread(ei, 1, EI_NIDENT, stdin) != EI_NIDENT) { fprintf(stderr, "Error: input truncated\n"); @@ -28,24 +27,6 @@ main(int argc, char **argv) default: exit(1); } - switch (ei[EI_DATA]) { - case ELFDATA2LSB: - printf("#define KERNEL_ELFDATA ELFDATA2LSB\n"); - break; - case ELFDATA2MSB: - printf("#define KERNEL_ELFDATA ELFDATA2MSB\n"); - break; - default: - exit(1); - } - - endian_test.s = 0x0102; - if (memcmp(endian_test.c, "\x01\x02", 2) == 0) - printf("#define HOST_ELFDATA ELFDATA2MSB\n"); - else if (memcmp(endian_test.c, "\x02\x01", 2) == 0) - printf("#define HOST_ELFDATA ELFDATA2LSB\n"); - else - exit(1); return 0; } diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index d16d0ace2775..f492fbeed300 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -50,6 +50,9 @@ static bool error_occurred; static bool extra_warn; +bool target_is_big_endian; +bool host_is_big_endian; + /* * Cut off the warnings when there are too many. This typically occurs when * vmlinux is missing. ('make modules' without building vmlinux.) @@ -438,6 +441,18 @@ static int parse_elf(struct elf_info *info, const char *filename) /* Not an ELF file - silently ignore it */ return 0; } + + switch (hdr->e_ident[EI_DATA]) { + case ELFDATA2LSB: + target_is_big_endian = false; + break; + case ELFDATA2MSB: + target_is_big_endian = true; + break; + default: + fatal("kernel endian is unknown\n"); + } + /* Fix endianness in ELF header */ hdr->e_type = TO_NATIVE(hdr->e_type); hdr->e_machine = TO_NATIVE(hdr->e_machine); @@ -2117,6 +2132,25 @@ struct dump_list { const char *file; }; +static void check_host_endian(void) +{ + static const union { + short s; + char c[2]; + } endian_test = { .c = {0x01, 0x02} }; + + switch (endian_test.s) { + case 0x0102: + host_is_big_endian = true; + break; + case 0x0201: + host_is_big_endian = false; + break; + default: + fatal("Unknown host endian\n"); + } +} + int main(int argc, char **argv) { struct module *mod; @@ -2181,6 +2215,8 @@ int main(int argc, char **argv) } } + check_host_endian(); + list_for_each_entry_safe(dl, dl2, &dump_lists, list) { read_dump(dl->file); list_del(&dl->list); diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 58197b34a3c8..54ba9431713f 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -62,15 +62,8 @@ x); \ }) -#if KERNEL_ELFDATA != HOST_ELFDATA - -#define TO_NATIVE(x) (bswap(x)) - -#else /* endianness matches */ - -#define TO_NATIVE(x) (x) - -#endif +#define TO_NATIVE(x) \ + (target_is_big_endian == host_is_big_endian ? x : bswap(x)) #define NOFAIL(ptr) do_nofail((ptr), #ptr) @@ -187,6 +180,8 @@ void add_moddevtable(struct buffer *buf, struct module *mod); void get_src_version(const char *modname, char sum[], unsigned sumlen); /* from modpost.c */ +extern bool target_is_big_endian; +extern bool host_is_big_endian; char *read_text_file(const char *filename); char *get_line(char **stringp); void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym); From patchwork Sat Jul 27 07:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13743628 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02FA4433C4; Sat, 27 Jul 2024 07:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066341; cv=none; b=p/DElrUXqDBJlQDIJoP80+R1fp2YfsoW9Zq4an2gVAVY5tpa55twVOIxvqf6uwZKthf+prjAG4Y9weXZK9lpb2IOwZZMCUnXvfy8L6bXPzunfEW+uJ+UDjIKX+Ry4xqltjB/4R1RbVo41+MST2F4MeSiFU1rcJrwDp8QLVlfu2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066341; c=relaxed/simple; bh=oKuHdUJvEMs8s+bKVfaeIF94aWnkZ0wZAGFLL/dcK1k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S8CN7/WyxOjCbv/frPHGsNG+43iR20BWD0ksRtbHDrqpkbc9n7b8TgyqM6Owuqn9gasAzOGLp/QrjTXmK66Bk5BO2Y8DyEqkSTBG45r9Q7rZFPTIYPIxp8VmxVW5lOA3kZ2iOBwksVKIsY+I8kyNo2aC6XYV4IEdfi4ZQvFPM3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dek7aBA/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dek7aBA/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC587C4AF07; Sat, 27 Jul 2024 07:45:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722066340; bh=oKuHdUJvEMs8s+bKVfaeIF94aWnkZ0wZAGFLL/dcK1k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dek7aBA/DG2+3sggGYpvUapEHPiERDv/vxnmydnCfYH86OGWfWjI9yYqAObAbGKg/ PTDkekoQf2HLfJGUHZ9/tsgAaxlsDWjjO8jm39pO9Y07BkldNbcb7Q1sd3PwLEfRji tlRT1FwHJtF3RbHVHDjoEg2t+nbXH7QGFy6M6U/hWtTWwSmkAzVkVZ5T9Zc0tPEb3E feP2WFZ+8l9q4C0V9qhJQQCb6jePr9caHSE9c8TSZ7dx0jgn3vpaOMUZfjY+6TILdp kr9T0PkNK8k+p3Vy81Frz4fILqqwvKl67/oSKPuBKh0Z7RuM0rc4P4/s9bMLoOdk6t qWw9eoTCROlzQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Kees Cook , Nathan Chancellor , Nicolas Schier , Ben Hutchings , Masahiro Yamada Subject: [PATCH 3/4] kbuild: slim down package for building external modules Date: Sat, 27 Jul 2024 16:42:03 +0900 Message-ID: <20240727074526.1771247-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240727074526.1771247-1-masahiroy@kernel.org> References: <20240727074526.1771247-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Exclude directories and files unnecessary for building external modules: - include/config/ (except include/config/auto.conf) - scripts/atomic/ - scripts/dtc/ - scripts/kconfig/ - scripts/mod/mk_elfconfig - scripts/package/ - scripts/unifdef - .config - *.o - .*.cmd Avoid copying files twice for the following directories: - include/generated/ - arch/*/include/generated/ Signed-off-by: Masahiro Yamada Reviewed-by: Nicolas Schier --- scripts/package/install-extmod-build | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/scripts/package/install-extmod-build b/scripts/package/install-extmod-build index 8cc9e13403ae..cc335945dfbc 100755 --- a/scripts/package/install-extmod-build +++ b/scripts/package/install-extmod-build @@ -9,15 +9,22 @@ is_enabled() { grep -q "^$1=y" include/config/auto.conf } +find_in_scripts() { + find scripts \ + \( -name atomic -o -name dtc -o -name kconfig -o -name package \) -prune -o \ + ! -name unifdef -a ! -name mk_elfconfig -a \( -type f -o -type l \) -print +} + mkdir -p "${destdir}" ( cd "${srctree}" echo Makefile find "arch/${SRCARCH}" -maxdepth 1 -name 'Makefile*' - find include scripts -type f -o -type l + find "arch/${SRCARCH}" -name generated -prune -o -name include -type d -print find "arch/${SRCARCH}" -name Kbuild.platforms -o -name Platform - find "arch/${SRCARCH}" -name include -type d + find include \( -name config -o -name generated \) -prune -o \( -type f -o -type l \) -print + find_in_scripts ) | tar -c -f - -C "${srctree}" -T - | tar -xf - -C "${destdir}" { @@ -25,12 +32,15 @@ mkdir -p "${destdir}" echo tools/objtool/objtool fi - find "arch/${SRCARCH}/include" Module.symvers include scripts -type f + echo Module.symvers + echo "arch/${SRCARCH}/include/generated" + echo include/config/auto.conf + echo include/generated + find_in_scripts if is_enabled CONFIG_GCC_PLUGINS; then find scripts/gcc-plugins -name '*.so' fi } | tar -c -f - -T - | tar -xf - -C "${destdir}" -# copy .config manually to be where it's expected to be -cp "${KCONFIG_CONFIG}" "${destdir}/.config" +find "${destdir}" \( -name '.*.cmd' -o -name '*.o' \) -delete From patchwork Sat Jul 27 07:42:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13743629 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 123CF4DA00; Sat, 27 Jul 2024 07:45:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066343; cv=none; b=S3ynIYAeRBwY92RTRm5O4QVnwdZ+nPvKLLly9jsbtBzm5DzTPDlor8nmlxRjAiYjYln9e30CM+P5+DkAQKNwLoKyZg9bRVqJ1Ub/dl+6tLtwMKqo1yeCdujBduv0hqQ0yKyE5dc5M6d7Aq7yFkb3QmNxQeaLH27sooYjTH4yIB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722066343; c=relaxed/simple; bh=bOu4ELquTW2+a3AqqRn9H85xVK64vtAr4KgLuMen9ts=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fMllQz+F+UibXci0zbVmEkxW+9TngU94cLoePyuT105wk+DHjhs1ZkSRowRTJxtE+ODPf6egxFq1a41Gb4DYPH/IXcMpJcGPqf+bPWZhI2QmRuty8oK8j1Vgz7z4+2Wjz0hTg5UmjHQyEJZ15RzmzLxzB4P9OAMrftbuZ/EzeD4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qlF5tbpj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qlF5tbpj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18F01C4AF09; Sat, 27 Jul 2024 07:45:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722066342; bh=bOu4ELquTW2+a3AqqRn9H85xVK64vtAr4KgLuMen9ts=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qlF5tbpjaG/qXOTD5qhOi+TX0+J8rTnbqVros8ZlP+yo5OhY2m9IaJv+d9l8c8E1R WvrwgnzoWH/vqjQVY+8Fjgxz/Vrf/Q/IOvpsFfcOiZiPKpcGtJNP3bczaAOP57sUJ6 PZnc+ajo3dAInygYLIzxDZHvecJoG9DIgF/tsUeuL7Qkio9I4e3mnSNWImjPNVAo/q yos60Ti/602Gw6AdiNT6ds7KQ4ir/uToQcNjG/+mD+ko6MuzJ7ZPP2Jsa6Gc8d7iKs aHurmPeA1dQ7dQRIBMYHvJwllCBp3kSmR8rG9fLu5k54QdUKibjj4qVyrvPt1ABZmG HZ3EBqiPfNcFQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Kees Cook , Nathan Chancellor , Nicolas Schier , Ben Hutchings , Masahiro Yamada Subject: [PATCH 4/4] kbuild: cross-compile linux-headers package when possible Date: Sat, 27 Jul 2024 16:42:04 +0900 Message-ID: <20240727074526.1771247-5-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240727074526.1771247-1-masahiroy@kernel.org> References: <20240727074526.1771247-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A long standing issue in the upstream kernel packaging is that the linux-headers package is not cross-compiled. For example, you can cross-build Debian packages for arm64 by running the following command: $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bindeb-pkg However, the generated linux-headers-*_arm64.deb is useless because the host programs in it were built for your build machine architecture (likely x86), not arm64. The Debian kernel maintains its own Makefiles to cross-compile host tools without relying on Kbuild. [1] Instead of adding such full custom Makefiles, this commit adds a small piece of code to cross-compile host programs located under the scripts/ directory. A straightforward solution is to pass HOSTCC=${CROSS_COMPILE}gcc, but it would also cross-compile scripts/basic/fixdep, which needs to be native to process the if_changed_dep macro. (This approach may work under some circumstances; you can execute foreign architecture programs with the help of binfmt_misc because Debian systems enable CONFIG_BINFMT_MISC, but it would require installing QEMU and libc for that architecture.) A trick is to use the external module build (KBUILD_EXTMOD=), which does not rebuild scripts/basic/fixdep. ${CC} needs to be able to link userspace programs (CONFIG_CC_CAN_LINK=y). There are known limitations: - GCC plugins It would possible to rebuild GCC plugins for the target architecture by passing HOSTCXX=${CROSS_COMPILE}g++ with necessary packages installed, but gcc on the installed system emits "cc1: error: incompatible gcc/plugin versions". I did not find a solution for this because 'gcc' on a foreign architecture is a different compiler after all. - objtool and resolve_btfids These are built by the tools build system. They are not covered by the current solution. I only tested this with Debian, but it should work for other package systems as well. [1]: https://salsa.debian.org/kernel-team/linux/-/blob/debian/6.9.9-1/debian/rules.real#L586 Signed-off-by: Masahiro Yamada Reviewed-by: Nicolas Schier --- scripts/package/install-extmod-build | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/scripts/package/install-extmod-build b/scripts/package/install-extmod-build index cc335945dfbc..0b56d3d7b48f 100755 --- a/scripts/package/install-extmod-build +++ b/scripts/package/install-extmod-build @@ -43,4 +43,38 @@ mkdir -p "${destdir}" fi } | tar -c -f - -T - | tar -xf - -C "${destdir}" +# When ${CC} and ${HOSTCC} differ, we are likely cross-compiling. Rebuild host +# programs using ${CC}. This assumes CC=${CROSS_COMPILE}gcc, which is usually +# the case for package building. It does not cross-compile when CC=clang. +# +# This caters to host programs that participate in Kbuild. objtool and +# resolve_btfids are out of scope. +if [ "${CC}" != "${HOSTCC}" ] && is_enabled CONFIG_CC_CAN_LINK; then + echo "Rebuilding host programs with ${CC}..." + + cat <<-'EOF' > "${destdir}/Kbuild" + subdir-y := scripts + EOF + + # HOSTCXX is not overridden. The C++ compiler is used to build: + # - scripts/kconfig/qconf, which is unneeded for external module builds + # - GCC plugins, which will not work on the installed system even with + # being rebuilt. + # + # Use the single-target build to avoid the modpost invocation, which + # would overwrite Module.symvers. + "${MAKE}" HOSTCC="${CC}" KBUILD_EXTMOD="${destdir}" scripts/ + + cat <<-'EOF' > "${destdir}/scripts/Kbuild" + subdir-y := basic + hostprogs-always-y := mod/modpost + mod/modpost-objs := $(addprefix mod/, modpost.o file2alias.o sumversion.o symsearch.o) + EOF + + # Run once again to rebuild scripts/basic/ and scripts/mod/modpost. + "${MAKE}" HOSTCC="${CC}" KBUILD_EXTMOD="${destdir}" scripts/ + + rm -f "${destdir}/Kbuild" "${destdir}/scripts/Kbuild" +fi + find "${destdir}" \( -name '.*.cmd' -o -name '*.o' \) -delete