From patchwork Tue Jan 16 17:25:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 861799 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zLcZc3mpnz9sQm for ; Wed, 17 Jan 2018 04:26:16 +1100 (AEDT) Received: from localhost ([::1]:50764 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebV02-0000Df-JD for incoming@patchwork.ozlabs.org; Tue, 16 Jan 2018 12:26:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36943) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebUzV-0000Aj-86 for qemu-devel@nongnu.org; Tue, 16 Jan 2018 12:25:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebUzR-0001Rj-CQ for qemu-devel@nongnu.org; Tue, 16 Jan 2018 12:25:41 -0500 Received: from mout.kundenserver.de ([212.227.126.131]:53531) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebUzR-0001Qs-1S for qemu-devel@nongnu.org; Tue, 16 Jan 2018 12:25:37 -0500 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue004 [212.227.15.167]) with ESMTPSA (Nemesis) id 0M2HkO-1esCSN46Jo-00s58X; Tue, 16 Jan 2018 18:25:16 +0100 From: Laurent Vivier To: qemu-devel@nongnu.org Date: Tue, 16 Jan 2018 18:25:08 +0100 Message-Id: <20180116172510.28878-3-laurent@vivier.eu> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180116172510.28878-1-laurent@vivier.eu> References: <20180116172510.28878-1-laurent@vivier.eu> X-Provags-ID: V03:K0:PZqPfo3/tpaZKKS0yG6IhNk8hB45MD5r/wDklPZ2kF92eejf+/6 tIrVg+uXtef2JS+WYtj299MnYmZvZupBwa2oefPyw3DfswHDz+5BAcD+qwszX8+jN9KqDot F5oMJY4OkOdhhr2WaqFH53oAI47nrvigwjDv1f6cMTOOx/nI46jyECBlmAxz5a5HhaGwfS6 B2ff+/OSY3ZY+QEaVpNzQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:TSHAGhvCx7g=:id6nfakILOKJvdhvPPXNIN 12VKatT+FglitKUVimEgkX2HcwttDnccq/tZt+KwFh8gar1Qxyy1ikLQFG6Ibr9YgbVZWwzTz Z0PACBCz8reUEmmhez37csOU96u/hewWcxL6rokw+OUXFBEZCtsZ/BU7tI1Q0it+fJ6E3mBwP MbeKbpB1xoSkwe9rg5CQsVsn76Uo9fu8EAlDtiInaKyxmPyJvhRYI4E12rJNbRjyusToR4CsV 3/zrL4kSHtPHLKwmETyrOHWC2w1htUlaRM1PuN/R/VJ1HcaM96avw1xkohgsxo7h3/7jsbjNa 93cqa0uBWyHi+uNWDUEZp9YR9nhXqKQbVSZb0efkeU8Uyo8g1mFj8WQu866G15/K5a0sIllsW breiZmj8DRI1rc5yseEYjqTZ+MhBCjDzlDtg6Ji3n48DVXdXq8quzWx7TI+gO1pPHHsynB4bz D4XayMf/5ZMNk2nhF2PMXUHk/qnRNLle/Xz5jFxs22YBWPmgMVHSsMuS86vnu6kHUM+Iguacs ePhESZtDRDGaCdjxB8sMRfMOcQBD3WGa+zDByoi6EU5HN43zyhDdGrBZveHQov4TpUfV6SaDC K94UIh6jnCl2o9pw2tE/oQNnNQjelrvjQ+d291bqvSbQ2hD6iLrBjw8IDvxJXqlucjUD6VL89 /soANj5D9GOGIMkVvN7qGfgLvTPCqIkeiDhhOYVMQpNOWdSmNkK9CFuwAIzuHDey6pzC/rIjX sVV1tJMYvJQUlUwbR0yJunDwaXwpt9VqJGTWxQ== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.126.131 Subject: [Qemu-devel] [PATCH v2 2/4] linux-user: introduce functions to detect CPU type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , YunQiang Su , Riku Voipio , Richard Henderson , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Laurent Vivier , Aaron Sierra Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: YunQiang Su Add a function to return ELF e_flags and use it to select the CPU model. [lv: split the patch and some cleanup in get_elf_eflags()] Signed-off-by: Laurent Vivier Reviewed-by: Richard Henderson --- Notes: YunQiang Su, please add your Signed-off-by that was missing in your original patch. v2: call cpu_get_model() with the result of get_elf_eflags() linux-user/elfload.c | 35 +++++++++++++++++++++++++++++++++++ linux-user/main.c | 20 ++++++++++---------- linux-user/qemu.h | 1 + 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 20f3d8c2c3..67e2425ac6 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2376,6 +2376,41 @@ give_up: g_free(syms); } +uint32_t get_elf_eflags(int fd) +{ + struct elfhdr ehdr; + off_t offset; + int ret; + + /* Read ELF header */ + offset = lseek(fd, 0, SEEK_SET); + if (offset == (off_t) -1) { + return 0; + } + ret = read(fd, &ehdr, sizeof(ehdr)); + if (ret < sizeof(ehdr)) { + return 0; + } + offset = lseek(fd, offset, SEEK_SET); + if (offset == (off_t) -1) { + return 0; + } + + /* Check ELF signature */ + if (!elf_check_ident(&ehdr)) { + return 0; + } + + /* check header */ + bswap_ehdr(&ehdr); + if (!elf_check_ehdr(&ehdr)) { + return 0; + } + + /* return architecture id */ + return ehdr.e_flags; +} + int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) { struct image_info interp_info; diff --git a/linux-user/main.c b/linux-user/main.c index 3954e8996b..6845b56030 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4319,8 +4319,17 @@ int main(int argc, char **argv, char **envp) init_qemu_uname_release(); + execfd = qemu_getauxval(AT_EXECFD); + if (execfd == 0) { + execfd = open(filename, O_RDONLY); + if (execfd < 0) { + printf("Error while loading %s: %s\n", filename, strerror(errno)); + _exit(EXIT_FAILURE); + } + } + if (cpu_model == NULL) { - cpu_model = cpu_get_model(0); + cpu_model = cpu_get_model(get_elf_eflags(execfd)); } tcg_exec_init(0); /* NOTE: we need to init the CPU at this stage to get @@ -4413,15 +4422,6 @@ int main(int argc, char **argv, char **envp) cpu->opaque = ts; task_settid(ts); - execfd = qemu_getauxval(AT_EXECFD); - if (execfd == 0) { - execfd = open(filename, O_RDONLY); - if (execfd < 0) { - printf("Error while loading %s: %s\n", filename, strerror(errno)); - _exit(EXIT_FAILURE); - } - } - ret = loader_exec(execfd, filename, target_argv, target_environ, regs, info, &bprm); if (ret != 0) { diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 4edd7d0c08..47ca71159c 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -188,6 +188,7 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp, struct target_pt_regs * regs, struct image_info *infop, struct linux_binprm *); +uint32_t get_elf_eflags(int fd); int load_elf_binary(struct linux_binprm *bprm, struct image_info *info); int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);