From patchwork Mon Oct 21 16:34:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 177122 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp3646784ill; Mon, 21 Oct 2019 09:35:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqznFxY0CXs80fWiLlm1lzUfWrU3fyz120M7MWvpSkcSMONVgk692HwhcXzC2wUNzTRj2/1W X-Received: by 2002:a17:906:d971:: with SMTP id rp17mr23369033ejb.42.1571675713060; Mon, 21 Oct 2019 09:35:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571675713; cv=none; d=google.com; s=arc-20160816; b=UPdm9kPekPqY2JXjMBMewCngk7ljBzC6G+KhlHlycMR/5C/2DJWWNGISfUHxSCNb+/ sh/evwihUSJfGEgPCsGfKhJXswr1Ukp+o/tmYcZOxwm+GiUo3eWat5BpsdJGCoA362AW 4mMvgGkAGhSzA0Voshi4duhUXl0S9A3Sn0C7KQyYK0hmFQJDsao39grbzDfDe6pqY+5A ayi8wugA1e4IVdaSjpOSgBnCHm34W8Jplc4w6F8GI0g05b1TLY3jfgEonlItSotHYSYQ eyY4CuZXk8f9new/LJRejc9T1KQxczhxOO1a6U5OKapirfkbU9vfUWSMGBHLUZsRhHYH MDbA== 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; bh=upMMg3q+g8+fj/Umcp722/DysQDkZfzDSMV9rpnsegk=; b=IRGlP2ZC3nRHdJrDa8XgKB8nawrhwRIy9WIGSg5gs1ymdP0fchu0+LoTJemLBKv1uj JV+thCXi41Xw9SfczGGdoT4CmPbz8zByVnV/+QOZ6qhUJQbiGfgbhVEvHC3QTFAKcm41 Ps1uTeilly/VsZuzOkq6mffxF1FonyDue4gls2POxMGqiGxqwV8oP1ydef2hy9EUyhOu T/X7xwWGMuJUdIcTO7GgwRTeG21oi/xpLhbl+MXpubC/f5iZh8cbGqZ82p3fg8gVsSQb rU6fCcfS4L6t54dp3OrZf4NIaY9vZE/qjIX/8jpi/Yw9VKKYPx31okizgfcOEEWHxfxE qjew== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c9si9548797edv.304.2019.10.21.09.35.12; Mon, 21 Oct 2019 09:35:13 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729125AbfJUQfF (ORCPT + 26 others); Mon, 21 Oct 2019 12:35:05 -0400 Received: from [217.140.110.172] ([217.140.110.172]:57580 "EHLO foss.arm.com" rhost-flags-FAIL-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1727805AbfJUQfE (ORCPT ); Mon, 21 Oct 2019 12:35:04 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 32A7611B3; Mon, 21 Oct 2019 09:34:43 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id EF0613F71F; Mon, 21 Oct 2019 09:34:40 -0700 (PDT) From: Mark Rutland To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: amit.kachhap@arm.com, ard.biesheuvel@linaro.org, catalin.marinas@arm.com, deller@gmx.de, duwe@suse.de, james.morse@arm.com, jeyu@kernel.org, jpoimboe@redhat.com, jthierry@redhat.com, mark.rutland@arm.com, mingo@redhat.com, peterz@infradead.org, rostedt@goodmis.org, svens@stackframe.org, takahiro.akashi@linaro.org, will@kernel.org Subject: [PATCH 3/8] arm64: module: rework special section handling Date: Mon, 21 Oct 2019 17:34:21 +0100 Message-Id: <20191021163426.9408-4-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20191021163426.9408-1-mark.rutland@arm.com> References: <20191021163426.9408-1-mark.rutland@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we load a module, we have to perform some special work for a couple of named sections. To do this, we iterate over all of the module's sections, and perform work for each section we recognize. To make it easier to handle the unexpected absence of a section, and to make the section-specific logic easer to read, let's factor the section search into a helper. Similar is already done in the core module loader, and other architectures (and ideally we'd unify these in future). If we expect a module to have an ftrace trampoline section, but it doesn't have one, we'll now reject loading the module. When ARM64_MODULE_PLTS is selected, any correctly built module should have one (and this is assumed by arm64's ftrace PLT code) and the absence of such a section implies something has gone wrong at build time. Subsequent patches will make use of the new helper. Signed-off-by: Mark Rutland Cc: Ard Bieshsheuvel Cc: Catalin Marinas Cc: James Morse Cc: Will Deacon --- arch/arm64/kernel/module.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) -- 2.11.0 diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index 03ff15bffbb6..763a86d52fef 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -470,22 +470,39 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, return -ENOEXEC; } -int module_finalize(const Elf_Ehdr *hdr, - const Elf_Shdr *sechdrs, - struct module *me) +static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + const char *name) { const Elf_Shdr *s, *se; const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) { - if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) - apply_alternatives_module((void *)s->sh_addr, s->sh_size); + if (strcmp(name, secstrs + s->sh_name) == 0) + return s; + } + + return NULL; +} + +int module_finalize(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + struct module *me) +{ + const Elf_Shdr *s; + + s = find_section(hdr, sechdrs, ".altinstructions"); + if (s) + apply_alternatives_module((void *)s->sh_addr, s->sh_size); + #ifdef CONFIG_ARM64_MODULE_PLTS - if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) && - !strcmp(".text.ftrace_trampoline", secstrs + s->sh_name)) - me->arch.ftrace_trampoline = (void *)s->sh_addr; -#endif + if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE)) { + s = find_section(hdr, sechdrs, ".text.ftrace_trampoline"); + if (!s) + return -ENOEXEC; + me->arch.ftrace_trampoline = (void *)s->sh_addr; } +#endif return 0; }