From patchwork Fri Jan 9 13:06:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Filippov X-Patchwork-Id: 427065 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from hemlock.osuosl.org (hemlock.osuosl.org [140.211.166.133]) by ozlabs.org (Postfix) with ESMTP id 74B22140192 for ; Sat, 10 Jan 2015 00:07:16 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 5AA5E920FA; Fri, 9 Jan 2015 13:07:15 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JVC6Maewm8UW; Fri, 9 Jan 2015 13:07:14 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by hemlock.osuosl.org (Postfix) with ESMTP id 3881E9209D; Fri, 9 Jan 2015 13:07:14 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from fraxinus.osuosl.org (fraxinus.osuosl.org [140.211.166.137]) by ash.osuosl.org (Postfix) with ESMTP id A770C1C22F5 for ; Fri, 9 Jan 2015 13:07:12 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id A448488671 for ; Fri, 9 Jan 2015 13:07:12 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bZfFeAHmp3z5 for ; Fri, 9 Jan 2015 13:07:12 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com [209.85.217.177]) by fraxinus.osuosl.org (Postfix) with ESMTPS id CCE7A8A013 for ; Fri, 9 Jan 2015 13:07:11 +0000 (UTC) Received: by mail-lb0-f177.google.com with SMTP id b6so7916831lbj.8 for ; Fri, 09 Jan 2015 05:07:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=E6nMn2WdmL91j47vG45L8uVms6jT0TrmyzP4x2mgqtE=; b=Sgt7MmYNhmiAKwS1USBgtNAwb4JymoTOb3eGVPqf9GkEx5tAgmy5+XrIeO4akVEEfF eNTdUCCQGZ4r2ArSoeP7kMdJQTg8fCiRNaCblaIW4e3c7zNvHgUx8mmqj6D7REioaq8y 5R7MyzUAYf/VcZ5MkaSZGesplr9t6mSY/ABav6PTJMeEHw9OoYhTTHrD7pPT2nizNNiU blhv/O5ma0EzfJcqCNrpHyFWaz7Va8wJR8d5AH6LPUI9KRWdGAdEjGwzF11f7PhGptpm gm/c0W46nfr9en8Cm2CQUcVY2OJO1gKuZkGbSIt9nExr6OtBQbUzVLTd1mCGbcLfrDF1 vLwQ== X-Received: by 10.152.18.225 with SMTP id z1mr21552514lad.0.1420808830136; Fri, 09 Jan 2015 05:07:10 -0800 (PST) Received: from octofox.metropolis ([5.19.183.212]) by mx.google.com with ESMTPSA id wq1sm1860550lbb.24.2015.01.09.05.07.08 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Jan 2015 05:07:09 -0800 (PST) From: Max Filippov To: uclibc@uclibc.org Subject: [PATCH v2] xtensa: ldso: coalesce dl_mprotect address ranges Date: Fri, 9 Jan 2015 16:06:59 +0300 Message-Id: <1420808819-22641-1-git-send-email-jcmvbkbc@gmail.com> X-Mailer: git-send-email 1.8.1.4 Cc: linux-xtensa@linux-xtensa.org X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "Discussion and development of uClibc \(the embedded C library\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: uclibc-bounces@uclibc.org Sender: "uClibc" This noticeably lowers the number of mprotect calls at program startup, e.g. for busybox: 7 calls vs 1835 calls. Signed-off-by: Max Filippov --- Changes v1->v2: - initialize prev_got_start and prev_got_end in INIT_GOT as well. ldso/ldso/xtensa/dl-startup.h | 18 +++++++++++++++++- ldso/ldso/xtensa/dl-sysdep.h | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ldso/ldso/xtensa/dl-startup.h b/ldso/ldso/xtensa/dl-startup.h index b135a4c..c60c9cf 100644 --- a/ldso/ldso/xtensa/dl-startup.h +++ b/ldso/ldso/xtensa/dl-startup.h @@ -83,6 +83,7 @@ do { \ unsigned long l_addr = tpnt->loadaddr; \ Elf32_Word relative_count; \ unsigned long rel_addr; \ + Elf32_Addr prev_got_start = 0, prev_got_end = 0; \ int x; \ \ got_loc = (xtensa_got_location *) \ @@ -93,7 +94,22 @@ do { \ got_start = got_loc[x].offset & ~(PAGE_SIZE - 1); \ got_end = ((got_loc[x].offset + got_loc[x].length + PAGE_SIZE - 1) \ & ~(PAGE_SIZE - 1)); \ - _dl_mprotect ((void *)(got_start + l_addr), got_end - got_start, \ + if (got_start >= prev_got_start && got_start <= prev_got_end) { \ + if (got_end > prev_got_end) \ + prev_got_end = got_end; \ + continue; \ + } else if (prev_got_start != prev_got_end) { \ + _dl_mprotect ((void *)(prev_got_start + l_addr), \ + prev_got_end - prev_got_start, \ + PROT_READ | PROT_WRITE | PROT_EXEC); \ + } \ + prev_got_start = got_start; \ + prev_got_end = got_end; \ + } \ +\ + if (prev_got_start != prev_got_end) { \ + _dl_mprotect ((void *)(prev_got_start + l_addr), \ + prev_got_end - prev_got_start, \ PROT_READ | PROT_WRITE | PROT_EXEC); \ } \ \ diff --git a/ldso/ldso/xtensa/dl-sysdep.h b/ldso/ldso/xtensa/dl-sysdep.h index a0ed4e5..958f7ef 100644 --- a/ldso/ldso/xtensa/dl-sysdep.h +++ b/ldso/ldso/xtensa/dl-sysdep.h @@ -36,6 +36,7 @@ typedef struct xtensa_got_location_struct { do { \ xtensa_got_location *got_loc; \ Elf32_Addr l_addr = MODULE->loadaddr; \ + Elf32_Addr prev_got_start = 0, prev_got_end = 0; \ int x; \ \ got_loc = (xtensa_got_location *) \ @@ -47,7 +48,26 @@ typedef struct xtensa_got_location_struct { got_start = got_loc[x].offset & ~(PAGE_SIZE - 1); \ got_end = ((got_loc[x].offset + got_loc[x].length + PAGE_SIZE - 1) \ & ~(PAGE_SIZE - 1)); \ - _dl_mprotect ((void *)(got_start + l_addr) , got_end - got_start, \ + if (got_start >= prev_got_start && got_start <= prev_got_end) \ + { \ + if (got_end > prev_got_end) \ + prev_got_end = got_end; \ + continue; \ + } \ + else if (prev_got_start != prev_got_end) \ + { \ + _dl_mprotect ((void *)(prev_got_start + l_addr), \ + prev_got_end - prev_got_start, \ + PROT_READ | PROT_WRITE | PROT_EXEC); \ + } \ + prev_got_start = got_start; \ + prev_got_end = got_end; \ + } \ + \ + if (prev_got_start != prev_got_end) \ + { \ + _dl_mprotect ((void *)(prev_got_start + l_addr), \ + prev_got_end - prev_got_start, \ PROT_READ | PROT_WRITE | PROT_EXEC); \ } \ \