From patchwork Mon Jul 2 18:11:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140840 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp247001ljj; Mon, 2 Jul 2018 11:12:10 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd2sGLUE8FWD/p7ZEEQzrut86Ojgf68bWys2qQKz6QQ4xPClrrVrvocuUSQTefG0HvxEMAh X-Received: by 2002:aa7:808f:: with SMTP id v15-v6mr9276553pff.38.1530555130415; Mon, 02 Jul 2018 11:12:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530555130; cv=none; d=google.com; s=arc-20160816; b=tP72GJxKirpsk2wpuOrK25tM5IByn+7JfZPp5e4DqDo0AsXztdG87IZqdEnesnMunk cL7nxsxuZp/zGcYGwaEe59q2WmPWkXunDC5mC86wOy8Q++39hjG/v6pUaFwB1pfj5V+U 3OFAzS6NrNvtTD6o7F/IejA+o1/mtjQTNBrmK55aBbg4f3yYo2cBX07xLah9oN4b0BbL 5TooSif/OZPqQX7kAaCM527hExZIJ1qWW7tsdj7eLHCB+/Os2V1WGNJ8oMq1ym5UB4vW QmsuJLotpLos615ETYVMIRME1ipMpvYRSMHDj3/RA0DZGWPed+92IMO94CVou4Arm0G6 Acuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=6nUyftJQHbn4ADCDRiHDYhO9k2WDA48zRvsOuVLWFCc=; b=LiIPbqbDW5n+7PYPzUQDZBkom+YCHE9Hi4QhxahRPoEheANcMuhkM8Knw/M3tZ/wKu XSGK0r6cV49A59X5ZREPCJS3mkMBfk9BR/RcLADeUmIsokC5V5Ck+7f1F+G3fV28CytX nIGiUKiHS5jzCPTAJa1Ca9OC0MCo/a7xbxy3ta87woDV3v+enMV4hPYzvOQD/QLEAMzJ h9Ys1/uhJl2rdm3Id/Wi8omPz07K/Ckgk4IRpWQF2oIZBsoRyUrk+zsbliB5f8krdqoL 3VuZ+l14n9wiT6dFZdRcYvTAyjl5hQt8gJaV10JjWkujTWCrDgFNdoCkMC7muL5hSPyK x8kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MH9B829V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q65-v6si11104938pga.283.2018.07.02.11.12.10; Mon, 02 Jul 2018 11:12:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MH9B829V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753408AbeGBSMI (ORCPT + 31 others); Mon, 2 Jul 2018 14:12:08 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:32779 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753348AbeGBSMD (ORCPT ); Mon, 2 Jul 2018 14:12:03 -0400 Received: by mail-wr0-f196.google.com with SMTP id k7-v6so12933931wrq.0 for ; Mon, 02 Jul 2018 11:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6nUyftJQHbn4ADCDRiHDYhO9k2WDA48zRvsOuVLWFCc=; b=MH9B829VsTLGEaymnuo3lvDs2YlPFP+tCJ4WAiPdSGavKSbuibs/57DY2V9s64nv+N Wu5/hzTyQX4OgnsjUsQWylcvIfyPpRPM9ImXRgbF/Vb7Wo4c3RDzobRfDcLQNlFTvAD/ CtHAKlxbhonAXmdbn45KBkVYQlAigW8Nd+8/0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6nUyftJQHbn4ADCDRiHDYhO9k2WDA48zRvsOuVLWFCc=; b=lhPa5z6jjIW2ecI0Fd/lKeCuqszsob0JyGsjzMzWuSNSy5oAe5ws3HVQDAeM/ZYA9i osF8wQshF4FnOrFtzDVhwm6Z8WX4bNEWRRlOMD/7bsk6zAuUeTWAsQYjH3EZxWGMvKYT noh3w2tLf9lTQ//s9Ec4UEcPqQ0mKqA2hvZNXPaVgW3DPkpP6Wycb+dSvr12Lx6XkExT pvDZhccisdUaSVOMhvb0qsTdlUisjBWxBI9FWZ6wCeU2jqvvwPW5Jo8PetbjQ3MqUw7a IopNMmvg6BclOt9cppJzunsA8uRlJTUGgqIkp/ASTPo4+UoQmU2hjLv6uKN38pQz4XYD 5T8g== X-Gm-Message-State: APt69E0mohtSR+B41t5k93SxrL+WfnzSKq0WRicnM7McMNeWHEvp0/f6 N+GjXyk8W4P3wRL8FiRlVhVuSQ== X-Received: by 2002:adf:f50e:: with SMTP id q14-v6mr76852wro.241.1530555121138; Mon, 02 Jul 2018 11:12:01 -0700 (PDT) Received: from localhost.localdomain (151.21.90.92.rev.sfr.net. [92.90.21.151]) by smtp.gmail.com with ESMTPSA id 189-v6sm10582822wmd.17.2018.07.02.11.11.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jul 2018 11:12:00 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Thomas Gleixner , Catalin Marinas , Ingo Molnar , Steven Rostedt , Martin Schwidefsky , Jessica Yu , Peter Zijlstra Subject: [PATCH v2 4/8] x86: add support for 64-bit place relative relocations Date: Mon, 2 Jul 2018 20:11:41 +0200 Message-Id: <20180702181145.4799-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180702181145.4799-1-ard.biesheuvel@linaro.org> References: <20180702181145.4799-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for R_X86_64_PC64 relocations, which operate on 64-bit quantities holding a relative symbol reference. This allows jump table entries to be emitted in a way that makes them invariant under runtime relocation, which means that no metadata needs to be emitted into the kernel image to describe such data structures, resulting in a size reduction. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/elf.h | 1 + arch/x86/kernel/machine_kexec_64.c | 4 ++++ arch/x86/kernel/module.c | 6 ++++++ arch/x86/tools/relocs.c | 10 ++++++++++ 4 files changed, 21 insertions(+) -- 2.17.1 diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h index 0d157d2a1e2a..d3925d684296 100644 --- a/arch/x86/include/asm/elf.h +++ b/arch/x86/include/asm/elf.h @@ -62,6 +62,7 @@ typedef struct user_fxsr_struct elf_fpxregset_t; #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */ #define R_X86_64_8 14 /* Direct 8 bit sign extended */ #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */ +#define R_X86_64_PC64 24 /* Place relative 64-bit signed */ #define R_X86_64_NUM 16 diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c index 4c8acdfdc5a7..6638d1edb2be 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -496,6 +496,10 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi, value -= (u64)address; *(u32 *)location = value; break; + case R_X86_64_PC64: + value -= (u64)address; + *(u64 *)location = value; + break; default: pr_err("Unknown rela relocation: %llu\n", ELF64_R_TYPE(rel[i].r_info)); diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index f58336af095c..b052e883dd8c 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -201,6 +201,12 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, goto overflow; #endif break; + case R_X86_64_PC64: + if (*(u64 *)loc != 0) + goto invalid_relocation; + val -= (u64)loc; + *(u64 *)loc = val; + break; default: pr_err("%s: Unknown rela relocation: %llu\n", me->name, ELF64_R_TYPE(rel[i].r_info)); diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 220e97841e49..a4075bc37e8f 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -195,6 +195,7 @@ static const char *rel_type(unsigned type) #if ELF_BITS == 64 REL_TYPE(R_X86_64_NONE), REL_TYPE(R_X86_64_64), + REL_TYPE(R_X86_64_PC64), REL_TYPE(R_X86_64_PC32), REL_TYPE(R_X86_64_GOT32), REL_TYPE(R_X86_64_PLT32), @@ -781,6 +782,15 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, add_reloc(&relocs32neg, offset); break; + case R_X86_64_PC64: + /* + * Only used by jump labels + */ + if (is_percpu_sym(sym, symname)) + die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n", + symname); + break; + case R_X86_64_32: case R_X86_64_32S: case R_X86_64_64: