From patchwork Wed Jan 28 01:00:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Whitehorn X-Patchwork-Id: 433747 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6E636140284 for ; Wed, 28 Jan 2015 12:25:34 +1100 (AEDT) Received: from ozlabs.org (ozlabs.org [103.22.144.67]) by lists.ozlabs.org (Postfix) with ESMTP id 502AF1A0735 for ; Wed, 28 Jan 2015 12:25:34 +1100 (AEDT) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org X-Greylist: delayed 1387 seconds by postgrey-1.35 at bilbo; Wed, 28 Jan 2015 12:25:29 AEDT Received: from anacreon.physics.berkeley.edu (anacreon.Physics.Berkeley.EDU [128.32.117.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id C747F1A06DD for ; Wed, 28 Jan 2015 12:25:29 +1100 (AEDT) Received: from anacreon.physics.berkeley.edu (localhost [127.0.0.1]) by anacreon.physics.berkeley.edu (8.14.9/8.14.9) with ESMTP id t0S10d5l024769 for ; Tue, 27 Jan 2015 17:00:39 -0800 (PST) (envelope-from nwhitehorn@freebsd.org) Message-ID: <54C834B6.7030809@freebsd.org> Date: Tue, 27 Jan 2015 17:00:38 -0800 From: Nathan Whitehorn User-Agent: Mozilla/5.0 (X11; FreeBSD powerpc; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: skiboot@lists.ozlabs.org Subject: [Skiboot] [PATCH v2] Allow ELF executables with function descriptor entrypoint to be skiboot payloads X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Big-endian ELF64 ELF executables normally (the Linux kernel is an exception) have their entry point refer to a function descriptor instead of the first instruction. Distinguish between the Linux case and the function descriptor case, which is used for the FreeBSD kernel, by checking whether the entry point points into an executable section or not. This allows use of the FreeBSD kernel as a skiboot payload. Signed-off-by: Nathan Whitehorn --- core/init.c | 22 ++++++++++++++++++++++ include/elf.h | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/core/init.c b/core/init.c index 2c7e30c..e75b5a3 100644 --- a/core/init.c +++ b/core/init.c @@ -111,6 +111,7 @@ static bool try_load_elf64(struct elf_hdr *header) struct elf64_hdr *kh = (struct elf64_hdr *)header; uint64_t load_base = (uint64_t)kh; struct elf64_phdr *ph; + struct elf64_shdr *sh; unsigned int i; /* Check it's a ppc64 LE ELF */ @@ -152,6 +153,27 @@ static bool try_load_elf64(struct elf_hdr *header) prerror("INIT: Failed to find kernel entry !\n"); return false; } + + /* For the normal big-endian ELF ABI, the kernel entry points + * to a function descriptor in the data section. Linux instead + * has it point directly to code. Test whether it is pointing + * into an executable section or not to figure this out. Default + * to assuming it obeys the ABI. + */ + sh = (struct elf64_shdr *)(load_base + kh->e_shoff); + for (i = 0; i < kh->e_shnum; i++, sh++) { + if (sh->sh_addr > kh->e_entry || + (sh->sh_addr + sh->sh_size) <= kh->e_entry) + continue; + + break; + } + + if (i == kh->e_shnum || !(sh->sh_flags & ELF_SFLAGS_X)) { + kernel_entry = *(uint64_t *)(kernel_entry + load_base); + kernel_entry = kernel_entry - ph->p_vaddr + ph->p_offset; + } + kernel_entry += load_base; kernel_32bit = false; diff --git a/include/elf.h b/include/elf.h index 0a52f3e..c600f7f 100644 --- a/include/elf.h +++ b/include/elf.h @@ -76,6 +76,23 @@ struct elf64_phdr { uint64_t p_align; }; +/* 64-bit ELF section header */ +struct elf64_shdr { + uint32_t sh_name; + uint32_t sh_type; + uint64_t sh_flags; +#define ELF_SFLAGS_X 0x4 +#define ELF_SFLAGS_A 0x2 +#define ELF_SFLAGS_W 0x1 + uint64_t sh_addr; + uint64_t sh_offset; + uint64_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint64_t sh_addralign; + uint64_t sh_entsize; +}; + /* Some relocation related stuff used in relocate.c */ struct elf64_dyn { int64_t d_tag;