From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 58264 invoked by alias); 19 Mar 2015 13:02:48 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 58038 invoked by uid 89); 19 Mar 2015 13:02:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL,BAYES_00,NO_DNS_FOR_FROM,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mga14.intel.com Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 19 Mar 2015 13:02:47 +0000 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP; 19 Mar 2015 06:02:44 -0700 X-ExtLoop1: 1 Received: from gnu-ivb-1.sc.intel.com ([172.25.70.165]) by FMSMGA003.fm.intel.com with ESMTP; 19 Mar 2015 06:02:44 -0700 Received: by gnu-ivb-1.sc.intel.com (Postfix, from userid 1000) id 9F9C91207EC; Thu, 19 Mar 2015 06:02:44 -0700 (PDT) Date: Thu, 19 Mar 2015 13:02:00 -0000 From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] Reduce file size for PT_GNU_RELRO segment Message-ID: <20150319130244.GA22592@intel.com> Reply-To: "H.J. Lu" MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-03/txt/msg00249.txt.bz2 When a padding in file is used to align PT_GNU_RELRO segment, the maximum padding size is maximum page size minus 1. This patch trades memory size for file size by increasing memory size to avoid padding if file size will be reduced by more than maximum page minus a page and memory size will be increased by less than a page. OK for master? Thanks. H.J. --- * ldlang.c (output_prev_load_sec_find): New. (lang_size_sections_1): Reduce file size for PT_GNU_RELRO segment by increasing memory size. --- ld/ldlang.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/ld/ldlang.c b/ld/ldlang.c index 8880821..3450c9d 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -1718,6 +1718,31 @@ output_prev_sec_find (lang_output_section_statement_type *os) return NULL; } +/* Find the last output load section with contents before given output + statement. */ + +static asection * +output_prev_load_sec_find (lang_output_section_statement_type *os) +{ + lang_output_section_statement_type *lookup; + + for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) + { + if (lookup->constraint < 0) + continue; + + if (lookup->bfd_section != NULL + && lookup->bfd_section->owner != NULL + && ((lookup->bfd_section->flags & (SEC_ALLOC + | SEC_LOAD + | SEC_HAS_CONTENTS)) + == (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS))) + return lookup->bfd_section; + } + + return NULL; +} + /* Look for a suitable place for a new output section statement. The idea is to skip over anything that might be inside a SECTIONS {} statement in a script, before we find another output section @@ -4776,6 +4801,12 @@ lang_size_sections_1 bfd_boolean check_regions) { lang_statement_union_type *s; + bfd_boolean reduce_padding + = (link_info.relro + && expld.dataseg.phase == exp_dataseg_relro_adjust + && expld.dataseg.maxpagesize > expld.dataseg.pagesize); + bfd_vma pagsizediff + = expld.dataseg.maxpagesize - expld.dataseg.pagesize; /* Size up the sections from their constituent parts. */ for (s = *prev; s != NULL; s = s->header.next) @@ -4918,6 +4949,38 @@ lang_size_sections_1 { bfd_vma savedot = newdot; newdot = align_power (newdot, section_alignment); + if (reduce_padding && expld.dataseg.base == newdot) + { + /* If a padding in file will be used for + PT_GNU_RELRO segment, check if we can reduce + the padding by increase the address of the + first relro section. It is a tradeoff between + memory size and file size. We only do it if + file size will be reduced by more than maximum + page minus a page and memory size will be + increased by less than a page. */ + asection *prev_sec + = output_prev_load_sec_find (os); + if (prev_sec) + { + bfd_vma pad + = ((newdot - (bfd_section_vma (prev_sec->owner, + prev_sec) + + bfd_get_section_size (prev_sec))) + % expld.dataseg.maxpagesize); + if (pad) + { + bfd_vma nextdot + = expld.dataseg.maxpagesize - pad; + nextdot = align_power (newdot + nextdot, + section_alignment); + if (((nextdot - newdot) + < expld.dataseg.pagesize) + && pad > pagsizediff) + newdot = nextdot; + } + } + } dotdelta = newdot - savedot; if (dotdelta != 0 -- 2.1.0