mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Fangrui Song <i@maskray.me>
To: Micha Nelissen <nelissen.micha@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: Re: size of executable
Date: Tue, 13 Aug 2019 11:45:43 +0000	[thread overview]
Message-ID: <20190813114251.jidhp3vgsvyidhnt@gmail.com> (raw)
In-Reply-To: <f99c8efc-9b8a-0963-f23d-32b343d49eaa@gmail.com>

On 2019-08-13, Micha Nelissen wrote:
>On 12-08-2019 20:23, Rich Felker wrote:
>>On Mon, Aug 12, 2019 at 11:16:39AM -0700, Khem Raj wrote:
>>>On Mon, Aug 12, 2019 at 10:19 AM Rich Felker <dalias@libc.org> wrote:
>>>>On Mon, Aug 12, 2019 at 05:55:28PM +0100, Jorge Almeida wrote:
>>>>>On Mon, Aug 12, 2019 at 5:48 PM Jorge Almeida <jjalmeida@gmail.com> wrote:
>>>>>>I get 16768 bytes (not stripped) and 12324 (stripped).
>>>>
>>>>This is a binutils regression from a dubious anti-ROP feature, -z
>>>>separate-code. Add -Wl,-z,noseparate-code and it will go away.
>>>
>>>is this still so with latest release as well.
>>
>>The breakage that caused separate-code to crash at runtime was fixed
>>between 2.31 and 2.32, but the size and performance regression
>>remains. With separate-code, a couple extra pages of memory and disk
>>are needed, with corresponding runtime cost to mmap them properly.
>>
>>All to avoid ROP gadgets, when every single dynamic-linked program has
>>a nice ROP gadget named "system" (among many others) in it...
>
>I'm curious. Jorge reports that the executable goes from 12k to 4k. 
>That suggests two pages saved? But if I look at documentation for this 
>separate-code option, then it says to allocate a separate code PT_LOAD 
>segment. (PT_LOAD just means loadable?) That would suggest up to 4k 
>more usage, not 8k right? One extra page necessary. Are by default 
>rodata and code combined but with separate-code those are separated? 
>Or something more happening?

binutils 2.31 includes a change "ld: Add --enable-separate-code". This is enabled by default for Linux x86.

You can compile a trivial program with -z separate-code and -z noseparate-code, run readelf -l and compare the results.

-z noseparate-code

  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x0007d8 0x0007d8 R E 0x1000
  LOAD           0x000e18 0x0000000000001e18 0x0000000000001e18 0x000210 0x000218 RW  0x1000

-z separate-code

  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000530 0x000530 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x0001cd 0x0001cd R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x000148 0x000148 R   0x1000
  LOAD           0x002e18 0x0000000000003e18 0x0000000000003e18 0x000210 0x000218 RW  0x1000

-z separate-code has two more PT_LOAD segments. What is bad is that the two PT_LOAD segments have aligned p_offset:

diff -u =(ld.bfd --verbose -z noseparate-code) =(ld.bfd --verbose -z separate-code)

+  . = ALIGN(CONSTANT (MAXPAGESIZE));

+  . = ALIGN(CONSTANT (MAXPAGESIZE));
+  /* Adjust the address for the rodata segment.  We want to adjust up to                                      
+     the same address within the page on the next page up.  */                                                
+  . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)));

This disables an important size optimization (I have some description in https://reviews.llvm.org/D64906)


      reply	other threads:[~2019-08-13 11:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-12 16:48 Jorge Almeida
2019-08-12 16:55 ` Jorge Almeida
2019-08-12 17:18   ` Rich Felker
2019-08-12 17:59     ` Jorge Almeida
2019-08-12 18:16     ` Khem Raj
2019-08-12 18:23       ` Rich Felker
2019-08-13 10:15         ` Micha Nelissen
2019-08-13 11:45           ` Fangrui Song [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190813114251.jidhp3vgsvyidhnt@gmail.com \
    --to=i@maskray.me \
    --cc=musl@lists.openwall.com \
    --cc=nelissen.micha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).