From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: (qmail 13592 invoked from network); 6 Apr 2020 22:04:22 -0000 Received-SPF: pass (mother.openwall.net: domain of lists.openwall.com designates 195.42.179.200 as permitted sender) receiver=inbox.vuxu.org; client-ip=195.42.179.200 envelope-from= Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with UTF8ESMTPZ; 6 Apr 2020 22:04:22 -0000 Received: (qmail 32523 invoked by uid 550); 6 Apr 2020 22:04:17 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 32505 invoked from network); 6 Apr 2020 22:04:16 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=3jZhS5g04bcOSwwOJHoh+LiStYmPbXImZK95xs0Aox4=; b=VdpsKzYD5DuHq05L4BrXu/DTsQGfpRvPGx2DJ/dDizqH0HTLsr5DKm9Cwes9NGiLok mP5pDyCqgAn2TOGvKml0yW517oLY24Aar88Q3E84/kju3H6g2/svwYNB4+wH9diazaQp Jhs+mOdVdR8ZTq7rGHSEFEth18e0+rKiprbI96k4XKUFFUkNGEPV6ZEgAzyNJIK5ByP2 SbMgJBYyQp24QqvmrKi+WImNtoZbjqyKFh19Bc0mNIzcjW7ulHFPEjdKwokxKVrUfGrE gi4Cgy9ooHb+M3tsJaeBkFkW7o4GV/+nQD6Ds/BBytOlV5TNNsIl+/oy7t4OvXjrcRQr MaCQ== X-Gm-Message-State: AGi0Pub75dC/9zXjiBw96f2RRsnxrDQ8UfX63Bs4p57yquNhfJ3ZL1lN eTbUumm9QdGL4W6q1e42YGc= X-Google-Smtp-Source: APiQypJD2xnnmKa919IaGLY5fvA9fg36AEqKLkdivzHfdSZwGhnj8vNehRXLWE6lrHoS8m1W2z1HSg== X-Received: by 2002:a63:705:: with SMTP id 5mr1250606pgh.314.1586210644997; Mon, 06 Apr 2020 15:04:04 -0700 (PDT) Date: Mon, 6 Apr 2020 15:04:03 -0700 From: Fangrui Song To: harald@redhat.com Cc: musl@lists.openwall.com Message-ID: <20200406220403.3moghw2llo34e6gh@gmail.com> References: <20200406180738.946894-1-harald@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200406180738.946894-1-harald@redhat.com> Subject: Re: [musl] [PATCH] V2 Enable linking to a static position independent executable On 2020-04-06, harald@redhat.com wrote: >From: Harald Hoyer > >This also enables address space layout randomization (ASLR). > >$ cat hello.c > >int main() >{ > printf("main = 0x%lxd\n", main); > return 0; >} > >$ gcc -static-pie -o hello hello.c -specs musl-gcc.specs > >$ ldd hello > statically linked > >$ file hello >hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=24fe0d02b0558dc37b8ae2268878b118804ae72d, with debug_info, not stripped > >$ ./hello >main = 0x7f858c4e72b9d > >$ ./hello >main = 0x7f0854d312b9d > >$ ./hello >main = 0x7f7179a1d2b9d > >$ ./hello >main = 0x7f37f981b2b9d > >$ readelf -l hello > >Elf file type is DYN (Shared object file) >Entry point 0x1058 >There are 9 program headers, starting at offset 64 > >Program Headers: > Type Offset VirtAddr PhysAddr > FileSiz MemSiz Flags Align > LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 > 0x00000000000003c0 0x00000000000003c0 R 0x1000 > LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 > 0x0000000000002cce 0x0000000000002cce R E 0x1000 > LOAD 0x0000000000004000 0x0000000000004000 0x0000000000004000 > 0x0000000000001550 0x0000000000001550 R 0x1000 > LOAD 0x0000000000005e50 0x0000000000006e50 0x0000000000006e50 > 0x00000000000002e0 0x00000000000009a0 RW 0x1000 > DYNAMIC 0x0000000000005e70 0x0000000000006e70 0x0000000000006e70 > 0x0000000000000170 0x0000000000000170 RW 0x8 > NOTE 0x0000000000000238 0x0000000000000238 0x0000000000000238 > 0x0000000000000024 0x0000000000000024 R 0x4 > GNU_EH_FRAME 0x0000000000004b60 0x0000000000004b60 0x0000000000004b60 > 0x00000000000001ec 0x00000000000001ec R 0x4 > GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 > 0x0000000000000000 0x0000000000000000 RW 0x10 > GNU_RELRO 0x0000000000005e50 0x0000000000006e50 0x0000000000006e50 > 0x00000000000001b0 0x00000000000001b0 R 0x1 > > Section to Segment mapping: > Segment Sections... > 00 .note.gnu.build-id .gnu.hash .dynsym .dynstr .rela.dyn > 01 .init .plt .text .fini > 02 .rodata .eh_frame_hdr .eh_frame > 03 .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .data .bss > 04 .dynamic > 05 .note.gnu.build-id > 06 .eh_frame_hdr > 07 > 08 .init_array .fini_array .data.rel.ro .dynamic .got >--- > tools/musl-gcc.specs.sh | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > mode change 100644 => 100755 tools/musl-gcc.specs.sh > >diff --git a/tools/musl-gcc.specs.sh b/tools/musl-gcc.specs.sh >old mode 100644 >new mode 100755 >index 30492574..72f5f487 >--- a/tools/musl-gcc.specs.sh >+++ b/tools/musl-gcc.specs.sh >@@ -8,7 +8,7 @@ cat < -nostdinc -isystem $incdir -isystem include%s %(old_cpp_options) > > *cc1: >-%(cc1_cpu) -nostdinc -isystem $incdir -isystem include%s >+%(cc1_cpu) %{static-pie:-fPIE} -nostdinc -isystem $incdir -isystem include%s The intention of the compiler driver option -static-pie is that it only affects linking options (-static -pie --no-dynamic-linker -z text), not compile options (-fPIE). (I don't know why -z text (which is not too relevant) ended up in GCC's default specs. Anyway, I followed suit for clang: https://reviews.llvm.org/D62606) > *link_libgcc: > -L$libdir -L .%s >@@ -17,13 +17,13 @@ cat < libgcc.a%s %:if-exists(libgcc_eh.a%s) > > *startfile: >-%{!shared: $libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s >+%{shared:;static:$libdir/crt1.o%s; static-pie:$libdir/rcrt1.o%s; pie:$libdir/Scrt1.o%s; :$libdir/crt1.o%s} $libdir/crti.o%s %{static:crtbeginT.o%s; shared|static-pie|pie:crtbeginS.o%s; :crtbegin.o%s} > > *endfile: >-crtendS.o%s $libdir/crtn.o >+%{static:crtend.o%s; shared|static-pie|pie:crtendS.o%s; :crtend.o%s} $libdir/crtn.o%s > > *link: >--dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{rdynamic:-export-dynamic} >+%{!r:--build-id} --no-add-needed %{!static|static-pie:--eh-frame-hdr} --hash-style=gnu %{shared:-shared} %{!shared:%{!static:%{!static-pie:%{rdynamic:-export-dynamic} -dynamic-linker $ldso}} %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}} > > *esp_link: > >-- >2.25.2 >