From patchwork Mon Aug 29 12:53:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 74911 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1618240qga; Mon, 29 Aug 2016 05:56:10 -0700 (PDT) X-Received: by 10.67.7.170 with SMTP id dd10mr32132181pad.152.1472475370148; Mon, 29 Aug 2016 05:56:10 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id n66si39136332pfi.70.2016.08.29.05.56.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Aug 2016 05:56:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1beM5i-0002W6-4P; Mon, 29 Aug 2016 12:55:06 +0000 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1beM58-0002Gf-Ml for linux-arm-kernel@lists.infradead.org; Mon, 29 Aug 2016 12:54:33 +0000 Received: by mail-wm0-x233.google.com with SMTP id q128so77400130wma.1 for ; Mon, 29 Aug 2016 05:54:10 -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=lB776XPfFjWmy/9XR2/mybtNSGIvhLbR9/NlySQ0Acw=; b=LCzy4w5+Vw8ByP19fxpqPubnNFCVI7h/xvuIT2hK/pviPvIs/n8711sx2yNVOfZIy2 X0VEjLV7kHx3Kk7mLEgzDNN88mnZTtybdj+wocL6F0zyzBnGpiDAIkYTQZ+jPv0s5LBl sMRwn5LD0MVNFdhdMDZQuqsLe7KfiaVNvo1TA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lB776XPfFjWmy/9XR2/mybtNSGIvhLbR9/NlySQ0Acw=; b=WQ38R2Wyoyh7BYUlrH0+J8UH6CFt7xgn3gj/8TJ1/nO0XjBKkUQZQi5/QRrvoZasle KlpK76L0ceQ/R3x83eAzydu6vHQzz0ovhdXmjkvyQxPI974I2ErPHb73lxad4ZROZ+ja b3Z3am614p3JT9pMjguW9KJyGUZGQJFCXZws30Zjbwxas/IA6Ce8pIckdLKZJnP1Cdsu vieASV8Lxqs0evIMZuJ9xO9Ezb3W7JHWaw2emyTThjlMuiTnOYiJ6w8FolVfBkoYKI1E igVC1Ahi4OaZa8RrfzjcB+kXmp+RBnZdwjBBdVdtOaov6wY3SQWIxwnqddQXh2Rp4EwZ 8hSQ== X-Gm-Message-State: AE9vXwOf86w9/LWFAREDXSS27vJbuClVJ+4VMvsCQdxWXncQEDCa7QQ6Gz7zDtXJnuP+jaao X-Received: by 10.194.134.234 with SMTP id pn10mr15046408wjb.35.1472475248895; Mon, 29 Aug 2016 05:54:08 -0700 (PDT) Received: from localhost.localdomain ([160.165.12.73]) by smtp.gmail.com with ESMTPSA id a3sm14656816wjw.15.2016.08.29.05.54.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 29 Aug 2016 05:54:08 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux@arm.linux.org.uk, dave.martin@arm.com Subject: [PATCH v4 2/4] ARM: kernel: allocate PLT entries only for external symbols Date: Mon, 29 Aug 2016 13:53:54 +0100 Message-Id: <1472475236-3083-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1472475236-3083-1-git-send-email-ard.biesheuvel@linaro.org> References: <1472475236-3083-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160829_055430_966189_A86AF84C X-CRM114-Status: GOOD ( 23.13 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:233 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: namhyung.kim@lge.com, youngho.shin@lge.com, arnd@arndb.de, Ard Biesheuvel , neidhard.kim@lge.com, chanho.min@lge.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org When CONFIG_ARM_MODULE_PLTS is enabled, jump and call instructions in modules no longer need to be within 16 MB (8 MB for Thumb2) of their targets. If they are further away, a PLT entry will be generated on the fly for each of them, which extends the range to the entire 32-bit address space. However, since these PLT entries will become the branch targets of the original jump and call instructions, the PLT itself needs to be in range, or we end up in the same situation we started in. Since the PLT is in a separate section, this essentially means that all jumps and calls inside the same module must be resolvable without PLT entries. The PLT allocation code executes before the module itself is loaded in its final location, and so it has to use a worst-case estimate for which jumps and calls will require an entry in the PLT at relocation time. As an optimization, this code deduplicates entries pointing to the same symbol, using a O(n^2) algorithm. However, it does not take the above into account, i.e., that PLT entries will only be needed for jump and call relocations against symbols that are not defined in the module. So disregard relocations against symbols that are defined in the module itself. As an additional minor optimization, ignore input sections that lack the SHF_EXECINSTR flag. Since jump and call relocations operate on executable instructions only, there is no need to look in sections that do not contain executable code. Tested-by: Jongsung Kim Signed-off-by: Ard Biesheuvel --- arch/arm/kernel/module-plts.c | 49 +++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) -- 2.7.4 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm/kernel/module-plts.c b/arch/arm/kernel/module-plts.c index 6832d1d6444e..6f93a905eeee 100644 --- a/arch/arm/kernel/module-plts.c +++ b/arch/arm/kernel/module-plts.c @@ -88,32 +88,45 @@ static int duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num, } /* Count how many PLT entries we may need */ -static unsigned int count_plts(Elf32_Addr base, const Elf32_Rel *rel, int num) +static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base, + const Elf32_Rel *rel, int num) { unsigned int ret = 0; + const Elf32_Sym *s; + u32 mask; int i; + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) + mask = __opcode_to_mem_thumb32(0x07ff2fff); + else + mask = __opcode_to_mem_arm(0x00ffffff); + /* * Sure, this is order(n^2), but it's usually short, and not * time critical */ - for (i = 0; i < num; i++) + for (i = 0; i < num; i++) { switch (ELF32_R_TYPE(rel[i].r_info)) { case R_ARM_CALL: case R_ARM_PC24: case R_ARM_JUMP24: - if (!duplicate_rel(base, rel, i, - __opcode_to_mem_arm(0x00ffffff))) - ret++; - break; -#ifdef CONFIG_THUMB2_KERNEL case R_ARM_THM_CALL: case R_ARM_THM_JUMP24: - if (!duplicate_rel(base, rel, i, - __opcode_to_mem_thumb32(0x07ff2fff))) + /* + * We only have to consider branch targets that resolve + * to undefined symbols. This is not simply a heuristic, + * it is a fundamental limitation, since the PLT itself + * is part of the module, and needs to be within range + * as well, so modules can never grow beyond that limit. + */ + s = syms + ELF32_R_SYM(rel[i].r_info); + if (s->st_shndx != SHN_UNDEF) + break; + + if (!duplicate_rel(base, rel, i, mask)) ret++; -#endif } + } return ret; } @@ -122,19 +135,27 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, { unsigned long plts = 0; Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum; + Elf32_Sym *syms = NULL; /* * To store the PLTs, we expand the .text section for core module code * and for initialization code. */ - for (s = sechdrs; s < sechdrs_end; ++s) + for (s = sechdrs; s < sechdrs_end; ++s) { if (strcmp(".plt", secstrings + s->sh_name) == 0) mod->arch.plt = s; + else if (s->sh_type == SHT_SYMTAB) + syms = (Elf32_Sym *)s->sh_addr; + } if (!mod->arch.plt) { pr_err("%s: module PLT section missing\n", mod->name); return -ENOEXEC; } + if (!syms) { + pr_err("%s: module symtab section missing\n", mod->name); + return -ENOEXEC; + } for (s = sechdrs + 1; s < sechdrs_end; ++s) { const Elf32_Rel *rels = (void *)ehdr + s->sh_offset; @@ -144,7 +165,11 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, if (s->sh_type != SHT_REL) continue; - plts += count_plts(dstsec->sh_addr, rels, numrels); + /* ignore relocations that operate on non-exec sections */ + if (!(dstsec->sh_flags & SHF_EXECINSTR)) + continue; + + plts += count_plts(syms, dstsec->sh_addr, rels, numrels); } mod->arch.plt->sh_type = SHT_NOBITS;