From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5681 Path: news.gmane.org!not-for-mail From: 'Rich Felker' Newsgroups: gmane.linux.lib.musl.general Subject: Re: static PIE Date: Wed, 30 Jul 2014 16:27:10 -0400 Message-ID: <20140730202710.GN1674@brightrain.aerifal.cx> References: <000001cfac2b$2030bd30$60923790$@codeaurora.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1406752052 18087 80.91.229.3 (30 Jul 2014 20:27:32 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Jul 2014 20:27:32 +0000 (UTC) Cc: musl@lists.openwall.com To: Weiming Zhao Original-X-From: musl-return-5686-gllmg-musl=m.gmane.org@lists.openwall.com Wed Jul 30 22:27:27 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1XCaT7-0000Gm-0R for gllmg-musl@plane.gmane.org; Wed, 30 Jul 2014 22:27:25 +0200 Original-Received: (qmail 3175 invoked by uid 550); 30 Jul 2014 20:27:24 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 3164 invoked from network); 30 Jul 2014 20:27:24 -0000 Content-Disposition: inline In-Reply-To: <000001cfac2b$2030bd30$60923790$@codeaurora.org> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:5681 Archived-At: On Wed, Jul 30, 2014 at 12:19:03PM -0700, Weiming Zhao wrote: > I just find a very interesting article written by you: > > http://www.openwall.com/lists/musl/2012/05/24/1 This method is somewhat outdated. In particular, requiring a custom linker script is a pain. The new method is to use -shared instead of -pie to trick gcc that it's generating a shared library (this will cause it to use a linker mode that does not add a PT_INTERP header, and to omit crt1) and manually add the needed Zcrt[12].o (no need to use -nostartfiles to suppress others). The command line should look like: gcc -shared -static-libgcc -Wl,-static -Wl,-Bsymbolic \ Zcrt1.o Zcrt2.o [your object files...] > I want to do the similar thing on ARM linux. I see _static_pie_reloc does > the relocation, which would be done by loader in dynamic PIE. Nice! Are you interested in trying to get this 'upstream' in gcc? Technically it's not needed, but it would be nice if "-pie -static" just did the right thing without the command line hackery. > But with "-static", those reloc entries has already been fixed by ld. > Without that, my code can still run but at fixed address space. I don't think that should happen. Static linking objects (as long as they're PIC/PIE) into a ET_DYN ELF file (.so or PIE executable) should not result in fixed addresses but "relative" type relocations for the dynamic linker. > To get the benefit of PIE, there should be address randomization (at least > for data sections), which should be done in startup code. Is my > understanding right? No, the kernel does the address randomization (the random base address it loads the program at). The userspace side is just applying this base address to the relative relocations in the rel/rela tables. Rich