From patchwork Mon Feb 19 17:10:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 875244 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 3zlVjc10j2z9ryy for ; Tue, 20 Feb 2018 04:14:42 +1100 (AEDT) Received: from localhost ([::1]:51451 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1enp1U-0007TE-AO for incoming@patchwork.ozlabs.org; Mon, 19 Feb 2018 12:14:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53717) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1enoxw-0004xH-2k for qemu-devel@nongnu.org; Mon, 19 Feb 2018 12:11:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1enoxs-0005VO-Mv for qemu-devel@nongnu.org; Mon, 19 Feb 2018 12:11:00 -0500 Received: from mout.kundenserver.de ([212.227.126.187]:59571) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1enoxs-0005Uj-C8 for qemu-devel@nongnu.org; Mon, 19 Feb 2018 12:10:56 -0500 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue001 [212.227.15.167]) with ESMTPSA (Nemesis) id 0LxGmY-1egwOc0YtW-016u2I; Mon, 19 Feb 2018 18:10:44 +0100 From: Laurent Vivier To: qemu-devel@nongnu.org Date: Mon, 19 Feb 2018 18:10:35 +0100 Message-Id: <20180219171037.24539-7-laurent@vivier.eu> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180219171037.24539-1-laurent@vivier.eu> References: <20180219171037.24539-1-laurent@vivier.eu> X-Provags-ID: V03:K0:JosostQ2jbfpzOBCEYrC1GYsavBcZCaK2Y1eoD3JIQ0SOg/jbsc ZCRA9zbM88Rt3XWw54tF1eXnLbGgsoehYccNAM5cRPL1DHMXokhH6ESwpvACgYYCE9TR0zt 2LdT9INJaGMt6I4PHRsizuL6brqcSIZrnuO/dVfGhYnGVgWOnY/AqrZhYudE8HXxM22V2s2 aqAuoFOmsrMzVoKFM0DRA== X-UI-Out-Filterresults: notjunk:1; V01:K0:mucI5Hdx34I=:kDQw45jQivdl5BzHhK5Zf9 ViYpaVBNQA2P/7FxN/ONIpthIVhFvvknodNG07iwRTPYEZu17YRkzCcFdEMBNbEcm5quG2NQC GIeRmbn50yLPCP3JB30XINLLsqMhs8uIvOS7NEznbDU5cVSWkjN30ZQoI2Xeu83803rOMxL9g PBM93akplNDN+N9EAFz128WTbeW3E1s0CDUgD6M/6AqPHywEM1eL3PMt5gLBdm9moGel3FKdP TKbzWv9+qD6n9yQUAkWKXq0ZFoiFblkl4MZUwQ4TUEIqGX/pFh23/GWbc0281Zs+wQcdb71qc O/o+vXgH2FF0LAWAcpP86GGibG5Q6oOUhuwzUXp1pePueIv0s6Ao7MROyj0hngwH8jf4iEK7v VIyVO48O1GJIXYP43zuCTG+OO+ZgALIsKKmkMo247zoi84lHME0lc8FY1movF9Y9lJo77vDh6 KC/zPSD4W4t93DbnGJnLqRY8xW8yn43C72xJ019+zIPu1m5r1fLA9JG4h4mfxSFtmQAqLXNie yN+qIY7YZrHolGZSHwUZjmubluBxU7SjIAfbPKLGKKlZMBDlqts+apia1C31MfSkhUHX3fTnb e3fcDY0nqvAYtYeiCN13IQ7QQU8O0azv05g4vHIYaBVpQNamkjwSYQ0aLpPuKrl+D0Awx9IwS MPx2FzQ5zHzKYEYtXyl1m0rJDa7BLGkBLu+d5EzcyiG6Ovr1MJ1mBDm0hQhfafVh/TsweYOT4 bwaiyqtXKKWDzii9q1bZxHziWTvx6+Hd4CPy8w== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.126.187 Subject: [Qemu-devel] [PULL 6/8] 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: YunQiang Su , Riku Voipio , Laurent Vivier 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. Signed-off-by: YunQiang Su [lv: split the patch and some cleanup in get_elf_eflags()] Signed-off-by: Laurent Vivier Reviewed-by: Richard Henderson Message-Id: <20180124211301.10095-3-laurent@vivier.eu> --- 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 8bb9a2c3e8..0208022445 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2396,6 +2396,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 079b98235b..bbeb78fb89 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4344,8 +4344,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 @@ -4438,15 +4447,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 bc4bf35036..f4b4ca72ad 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -186,6 +186,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);