From patchwork Thu Aug 11 23:08:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 658412 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3s9P7g3yfPz9s5g for ; Fri, 12 Aug 2016 09:17:51 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3s9P7g38p3zDsFr for ; Fri, 12 Aug 2016 09:17:51 +1000 (AEST) X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 3s9NxC40GCzDr2s for ; Fri, 12 Aug 2016 09:08:47 +1000 (AEST) Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u7BN8gRb072748 for ; Thu, 11 Aug 2016 19:08:45 -0400 Received: from e24smtp01.br.ibm.com (e24smtp01.br.ibm.com [32.104.18.85]) by mx0a-001b2d01.pphosted.com with ESMTP id 24rtx3kwd9-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 11 Aug 2016 19:08:44 -0400 Received: from localhost by e24smtp01.br.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 11 Aug 2016 20:08:42 -0300 Received: from d24dlp02.br.ibm.com (9.18.248.206) by e24smtp01.br.ibm.com (10.172.0.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 11 Aug 2016 20:08:40 -0300 X-IBM-Helo: d24dlp02.br.ibm.com X-IBM-MailFrom: bauerman@linux.vnet.ibm.com X-IBM-RcptTo: linuxppc-dev@lists.ozlabs.org Received: from d24relay02.br.ibm.com (d24relay02.br.ibm.com [9.13.184.26]) by d24dlp02.br.ibm.com (Postfix) with ESMTP id 0F2A71DC006D for ; Thu, 11 Aug 2016 19:08:31 -0400 (EDT) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.8.31.91]) by d24relay02.br.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u7BN8eqd29032866 for ; Thu, 11 Aug 2016 20:08:40 -0300 Received: from d24av01.br.ibm.com (localhost [127.0.0.1]) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u7BN8ccw027238 for ; Thu, 11 Aug 2016 20:08:39 -0300 Received: from hactar.ibm.com (mwramos.br.ibm.com [9.18.201.183] (may be forged)) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u7BN8S3r026883; Thu, 11 Aug 2016 20:08:37 -0300 From: Thiago Jung Bauermann To: kexec@lists.infradead.org Subject: [PATCH v5 06/13] powerpc: Adapt elf64_apply_relocate_add for kexec_file_load. Date: Thu, 11 Aug 2016 20:08:11 -0300 X-Mailer: git-send-email 1.9.1 In-Reply-To: <1470956898-5991-1-git-send-email-bauerman@linux.vnet.ibm.com> References: <1470956898-5991-1-git-send-email-bauerman@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16081123-1523-0000-0000-000002130021 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16081123-1524-0000-0000-000027CC00A3 Message-Id: <1470956898-5991-7-git-send-email-bauerman@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-08-11_14:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1608110298 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stewart Smith , Baoquan He , linuxppc-dev@lists.ozlabs.org, x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Ingo Molnar , Paul Mackerras , Eric Biederman , Thiago Jung Bauermann , Thomas Gleixner , Dave Young , Andrew Morton , Vivek Goyal Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" Extend elf64_apply_relocate_add to support relative symbols. This is necessary because there is a difference between how the module loading mechanism and the kexec purgatory loading code use Elf64_Sym.st_value at relocation time: the former changes st_value to point to the absolute memory address before relocating the module, while the latter does that adjustment during relocation of the purgatory. Also, add a check_symbols argument so that the kexec code can be stricter about undefined symbols. Finally, add relocation types used by the purgatory. Signed-off-by: Thiago Jung Bauermann --- arch/powerpc/include/asm/elf_util.h | 1 + arch/powerpc/kernel/elf_util_64.c | 84 ++++++++++++++++++++++++++++++++++++- arch/powerpc/kernel/module_64.c | 5 ++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/include/asm/elf_util.h b/arch/powerpc/include/asm/elf_util.h index a012ba03282d..3405eeabe542 100644 --- a/arch/powerpc/include/asm/elf_util.h +++ b/arch/powerpc/include/asm/elf_util.h @@ -67,6 +67,7 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, const char *strtab, const Elf64_Rela *rela, unsigned int num_rela, void *syms_base, void *loc_base, Elf64_Addr addr_base, + bool relative_symbols, bool check_symbols, const char *obj_name); #endif /* _ASM_POWERPC_ELF_UTIL_H */ diff --git a/arch/powerpc/kernel/elf_util_64.c b/arch/powerpc/kernel/elf_util_64.c index 8e5d400ac9f2..80f209a42abd 100644 --- a/arch/powerpc/kernel/elf_util_64.c +++ b/arch/powerpc/kernel/elf_util_64.c @@ -74,6 +74,8 @@ static void squash_toc_save_inst(const char *name, unsigned long addr) { } * @syms_base: Contents of the associated symbol table. * @loc_base: Contents of the section to which relocations apply. * @addr_base: The address where the section will be loaded in memory. + * @relative_symbols: Are the symbols' st_value members relative? + * @check_symbols: Fail if an unexpected symbol is found? * @obj_name: The name of the ELF binary, for information messages. * * Applies RELA relocations to an ELF file already at its final location @@ -84,11 +86,13 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, const char *strtab, const Elf64_Rela *rela, unsigned int num_rela, void *syms_base, void *loc_base, Elf64_Addr addr_base, + bool relative_symbols, bool check_symbols, const char *obj_name) { unsigned int i; unsigned long *location; unsigned long address; + unsigned long sec_base; unsigned long value; const char *name; Elf64_Sym *sym; @@ -121,8 +125,36 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, name, (unsigned long)sym->st_value, (long)rela[i].r_addend); + if (check_symbols) { + /* + * TOC symbols appear as undefined but should be + * resolved as well, so allow them to be processed. + */ + if (sym->st_shndx == SHN_UNDEF && + strcmp(name, ".TOC.") != 0) { + pr_err("Undefined symbol: %s\n", name); + return -ENOEXEC; + } else if (sym->st_shndx == SHN_COMMON) { + pr_err("Symbol '%s' in common section.\n", name); + return -ENOEXEC; + } + } + + if (relative_symbols && sym->st_shndx != SHN_ABS) { + if (sym->st_shndx >= elf_info->ehdr->e_shnum) { + pr_err("Invalid section %d for symbol %s\n", + sym->st_shndx, name); + return -ENOEXEC; + } else { + struct elf_shdr *sechdrs = elf_info->sechdrs; + + sec_base = sechdrs[sym->st_shndx].sh_addr; + } + } else + sec_base = 0; + /* `Everything is relative'. */ - value = sym->st_value + rela[i].r_addend; + value = sym->st_value + sec_base + rela[i].r_addend; switch (ELF64_R_TYPE(rela[i].r_info)) { case R_PPC64_ADDR32: @@ -135,6 +167,10 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, *(unsigned long *)location = value; break; + case R_PPC64_REL32: + *(uint32_t *)location = value - (uint32_t)(uint64_t)location; + break; + case R_PPC64_TOC: *(unsigned long *)location = my_r2(elf_info); break; @@ -186,6 +222,14 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, | (value & 0xfffc); break; + case R_PPC64_TOC16_HI: + /* Subtract TOC pointer */ + value -= my_r2(elf_info); + value = value >> 16; + *((uint16_t *) location) + = (*((uint16_t *) location) & ~0xffff) + | (value & 0xffff); + case R_PPC64_TOC16_HA: /* Subtract TOC pointer */ value -= my_r2(elf_info); @@ -195,6 +239,21 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, | (value & 0xffff); break; + case R_PPC64_REL14: + /* Convert value to relative */ + value -= address; + if (value + 0x8000 > 0xffff || (value & 3) != 0) { + pr_err("%s: REL14 %li out of range!\n", obj_name, + (long int)value); + return -ENOEXEC; + } + + /* Only replace bits 2 through 16 */ + *(uint32_t *)location + = (*(uint32_t *)location & ~0xfffc) + | (value & 0xfffc); + break; + case R_PPC_REL24: /* FIXME: Handle weak symbols here --RR */ if (sym->st_shndx == SHN_UNDEF) { @@ -263,6 +322,29 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value); break; + case R_PPC64_ADDR16_LO: + *(uint16_t *)location = value & 0xffff; + break; + + case R_PPC64_ADDR16_HI: + *(uint16_t *)location = (value >> 16) & 0xffff; + break; + + case R_PPC64_ADDR16_HA: + *(uint16_t *)location = (((value + 0x8000) >> 16) & + 0xffff); + break; + + case R_PPC64_ADDR16_HIGHER: + *(uint16_t *)location = (((uint64_t)value >> 32) & + 0xffff); + break; + + case R_PPC64_ADDR16_HIGHEST: + *(uint16_t *)location = (((uint64_t)value >> 48) & + 0xffff); + break; + case R_PPC64_REL16_HA: /* Subtract location pointer */ value -= address; diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c index 590e2413974f..10745ac6b004 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -443,6 +443,9 @@ int restore_r2(u32 *instruction, const char *obj_name) * When this function is called, the module is already at its final location in * memory, so Elf64_Shdr.sh_addr can be used for accessing the section * contents as well as the base address for relocations. + * + * Also, simplify_symbols already changed all symbols' st_value members + * to absolute addresses. */ int apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, @@ -472,7 +475,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, return elf64_apply_relocate_add(&me->arch.elf_info, strtab, rela, num_rela, syms_base, (void *) addr_base, - addr_base, me->name); + addr_base, false, false, me->name); } #ifdef CONFIG_DYNAMIC_FTRACE