From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11100 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Sturm Newsgroups: gmane.linux.lib.musl.general Subject: Static PIE with musl and clang Date: Sat, 4 Mar 2017 19:47:31 -0600 (CST) Message-ID: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2313_2122165615.1488678451059" X-Trace: blaine.gmane.org 1488678469 3931 195.159.176.226 (5 Mar 2017 01:47:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 5 Mar 2017 01:47:49 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-11115-gllmg-musl=m.gmane.org@lists.openwall.com Sun Mar 05 02:47:45 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ckLGx-0000Qk-PN for gllmg-musl@m.gmane.org; Sun, 05 Mar 2017 02:47:43 +0100 Original-Received: (qmail 19911 invoked by uid 550); 5 Mar 2017 01:47:46 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 19881 invoked from network); 5 Mar 2017 01:47:44 -0000 X-Virus-Scanned: amavisd-new at server03.computervoice.com X-Mailer: Zimbra 8.6.0_GA_1153 (ZimbraWebClient - GC51 (Win)/8.6.0_GA_1153) Thread-Topic: Static PIE with musl and clang Thread-Index: NxnXf3oHeMEehvVuQCS0NKi8oJGZrQ== Xref: news.gmane.org gmane.linux.lib.musl.general:11100 Archived-At: ------=_Part_2313_2122165615.1488678451059 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Has anyone managed to build static PIE with musl and clang, preferably linking with lld? I see that this has been done using GCC and rcrt1.o, but I cannot find a combination of command line parameters that will successfully link and execute with clang. I am using clang 4.0.0RC2 and musl 1.1.16. I have also tried to use binutils ld instead of llvm lld to no avail. ------=_Part_2313_2122165615.1488678451059 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
Has anyone managed to build static PIE with musl and clang, preferably linking with lld?

I see that this has been done using GCC and rcrt1.o, but I cannot find a combination of command line parameters that will successfully link and execute with clang.

I am using clang 4.0.0RC2 and musl 1.1.16. I have also tried to use binutils ld instead of llvm lld to no avail.
------=_Part_2313_2122165615.1488678451059-- From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11101 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Static PIE with musl and clang Date: Sat, 4 Mar 2017 21:44:03 -0500 Message-ID: <20170305024403.GK1520@brightrain.aerifal.cx> References: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1488681857 28371 195.159.176.226 (5 Mar 2017 02:44:17 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 5 Mar 2017 02:44:17 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-11116-gllmg-musl=m.gmane.org@lists.openwall.com Sun Mar 05 03:44:13 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ckM9c-0006zx-TT for gllmg-musl@m.gmane.org; Sun, 05 Mar 2017 03:44:13 +0100 Original-Received: (qmail 7760 invoked by uid 550); 5 Mar 2017 02:44: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: Original-Received: (qmail 7733 invoked from network); 5 Mar 2017 02:44:16 -0000 Content-Disposition: inline In-Reply-To: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:11101 Archived-At: On Sat, Mar 04, 2017 at 07:47:31PM -0600, Paul Sturm wrote: > Has anyone managed to build static PIE with musl and clang, > preferably linking with lld? > > I see that this has been done using GCC and rcrt1.o, but I cannot > find a combination of command line parameters that will successfully > link and execute with clang. > > I am using clang 4.0.0RC2 and musl 1.1.16. I have also tried to use > binutils ld instead of llvm lld to no avail. I doubt lld can do it yet; you need some equivalent of the --no-dynamic-linker option I added to BFD ld. It should be possible with clang using binutils/BFD ld. You might also be able to hack it by using -shared -Bsymbolic and explicitly linking rcrt1.o on the link command line (essentially doing the same thing you'd do to make an executable shared library) but there are corner-case things that break when you try to do it this way. The right approach would be adding --no-dynamic-linker to lld and teaching clang to pass it when linking with -static -pie. Rich From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11102 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Sturm Newsgroups: gmane.linux.lib.musl.general Subject: Re: Static PIE with musl and clang Date: Sat, 4 Mar 2017 21:41:24 -0600 (CST) Message-ID: <640524050.2626.1488685284594.JavaMail.zimbra@computervoice.com> References: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> <20170305024403.GK1520@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2625_477381364.1488685284590" X-Trace: blaine.gmane.org 1488685306 10362 195.159.176.226 (5 Mar 2017 03:41:46 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 5 Mar 2017 03:41:46 +0000 (UTC) To: musl Original-X-From: musl-return-11117-gllmg-musl=m.gmane.org@lists.openwall.com Sun Mar 05 04:41:41 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ckN3B-0001oB-Gq for gllmg-musl@m.gmane.org; Sun, 05 Mar 2017 04:41:37 +0100 Original-Received: (qmail 26481 invoked by uid 550); 5 Mar 2017 03:41:42 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26454 invoked from network); 5 Mar 2017 03:41:41 -0000 X-Virus-Scanned: amavisd-new at server03.computervoice.com In-Reply-To: <20170305024403.GK1520@brightrain.aerifal.cx> X-Mailer: Zimbra 8.6.0_GA_1153 (ZimbraWebClient - FF47 (Linux)/8.6.0_GA_1153) Thread-Topic: Static PIE with musl and clang Thread-Index: bDuR9eqyIXgO0NYo4e4FoMtkl/8xDw== Xref: news.gmane.org gmane.linux.lib.musl.general:11102 Archived-At: ------=_Part_2625_477381364.1488685284590 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit OK. I tried to compile this program: #include int main() { printf("%p\n", main); return 0; } Command line: clang /usr/lib/rcrt1.o ../aslr.c -o aslr -fPIE -shared -Bsymbolic -Wl,--no-dynamic-linker ./aslr seg faults when I run it. readelf -e aslr looks like this: ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x788 Start of program headers: 64 (bytes into file) Start of section headers: 6584 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 6 Size of section headers: 64 (bytes) Number of section headers: 26 Section header string table index: 23 Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .hash HASH 0000000000000190 00000190 0000000000000058 0000000000000004 A 2 0 8 [ 2] .dynsym DYNSYM 00000000000001e8 000001e8 0000000000000198 0000000000000018 A 3 1 8 [ 3] .dynstr STRTAB 0000000000000380 00000380 00000000000000de 0000000000000000 A 0 0 1 [ 4] .rela.dyn RELA 0000000000000460 00000460 00000000000000f0 0000000000000018 A 2 0 8 [ 5] .rela.plt RELA 0000000000000550 00000550 0000000000000048 0000000000000018 AI 2 19 8 [ 6] .init PROGBITS 0000000000000598 00000598 000000000000000d 0000000000000000 AX 0 0 1 [ 7] .plt PROGBITS 00000000000005b0 000005b0 0000000000000040 0000000000000010 AX 0 0 16 [ 8] .plt.got PROGBITS 00000000000005f0 000005f0 0000000000000018 0000000000000000 AX 0 0 8 [ 9] .text PROGBITS 0000000000000610 00000610 0000000000000497 0000000000000000 AX 0 0 16 [10] .fini PROGBITS 0000000000000aa7 00000aa7 0000000000000008 0000000000000000 AX 0 0 1 [11] .rodata PROGBITS 0000000000000aaf 00000aaf 0000000000000004 0000000000000001 AMS 0 0 1 [12] .eh_frame_hdr PROGBITS 0000000000000ab4 00000ab4 000000000000001c 0000000000000000 A 0 0 4 [13] .eh_frame PROGBITS 0000000000000ad0 00000ad0 0000000000000064 0000000000000000 A 0 0 8 [14] .ctors PROGBITS 0000000000200e40 00000e40 0000000000000010 0000000000000000 WA 0 0 8 [15] .dtors PROGBITS 0000000000200e50 00000e50 0000000000000010 0000000000000000 WA 0 0 8 [16] .jcr PROGBITS 0000000000200e60 00000e60 0000000000000008 0000000000000000 WA 0 0 8 [17] .dynamic DYNAMIC 0000000000200e68 00000e68 0000000000000150 0000000000000010 WA 3 0 8 [18] .got PROGBITS 0000000000200fb8 00000fb8 0000000000000048 0000000000000008 WA 0 0 8 [19] .got.plt PROGBITS 0000000000201000 00001000 0000000000000030 0000000000000008 WA 0 0 8 [20] .data PROGBITS 0000000000201030 00001030 0000000000000008 0000000000000000 WA 0 0 8 [21] .bss NOBITS 0000000000201040 00001038 0000000000000050 0000000000000000 WA 0 0 32 [22] .comment PROGBITS 0000000000000000 00001038 000000000000003c 0000000000000001 MS 0 0 1 [23] .shstrtab STRTAB 0000000000000000 000018fb 00000000000000b9 0000000000000000 0 0 1 [24] .symtab SYMTAB 0000000000000000 00001078 0000000000000648 0000000000000018 25 51 8 [25] .strtab STRTAB 0000000000000000 000016c0 000000000000023b 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), l (large), p (processor specific) Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000b34 0x0000000000000b34 R E 200000 LOAD 0x0000000000000e40 0x0000000000200e40 0x0000000000200e40 0x00000000000001f8 0x0000000000000250 RW 200000 DYNAMIC 0x0000000000000e68 0x0000000000200e68 0x0000000000200e68 0x0000000000000150 0x0000000000000150 RW 8 GNU_EH_FRAME 0x0000000000000ab4 0x0000000000000ab4 0x0000000000000ab4 0x000000000000001c 0x000000000000001c R 4 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 10 GNU_RELRO 0x0000000000000e40 0x0000000000200e40 0x0000000000200e40 0x00000000000001c0 0x00000000000001c0 R 1 Section to Segment mapping: Segment Sections... 00 .hash .dynsym .dynstr .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame 01 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 02 .dynamic 03 .eh_frame_hdr 04 05 .ctors .dtors .jcr .dynamic .got From: "dalias" To: "musl" Sent: Sunday, March 5, 2017 2:44:03 AM Subject: Re: [musl] Static PIE with musl and clang On Sat, Mar 04, 2017 at 07:47:31PM -0600, Paul Sturm wrote: > Has anyone managed to build static PIE with musl and clang, > preferably linking with lld? > > I see that this has been done using GCC and rcrt1.o, but I cannot > find a combination of command line parameters that will successfully > link and execute with clang. > > I am using clang 4.0.0RC2 and musl 1.1.16. I have also tried to use > binutils ld instead of llvm lld to no avail. I doubt lld can do it yet; you need some equivalent of the --no-dynamic-linker option I added to BFD ld. It should be possible with clang using binutils/BFD ld. You might also be able to hack it by using -shared -Bsymbolic and explicitly linking rcrt1.o on the link command line (essentially doing the same thing you'd do to make an executable shared library) but there are corner-case things that break when you try to do it this way. The right approach would be adding --no-dynamic-linker to lld and teaching clang to pass it when linking with -static -pie. Rich ------=_Part_2625_477381364.1488685284590 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
OK. I tried to compile this program:

#include <stdio.h>
int main() {
  printf("%p\n", main);
  return 0;
}

Command line:
clang= /usr/lib/rcrt1.o ../aslr.c -o aslr -fPIE -shared -Bsymbolic -Wl,--no-dynam= ic-linker

./aslr seg faults w= hen I run it.

<= /div>
readelf -e aslr looks like this:

ELF Header:
Magic: 7f 45 4c 46 0= 2 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's compleme= nt, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
= ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Mic= ro Devices X86-64
Version: 0x1
Entry point address: 0x788
Start= of program headers: 64 (bytes into file)
Start of section headers: 658= 4 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
= Size of program headers: 56 (bytes)
Number of program headers: 6
Si= ze of section headers: 64 (bytes)
Number of section headers: 26
Sec= tion header string table index: 23

Section Headers:
[Nr] Name Ty= pe Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000= 000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] = .hash HASH 0000000000000190 00000190
0000000000000058 0000000000000004 = A 2 0 8
[ 2] .dynsym DYNSYM 00000000000001e8 000001e8
0000000000000= 198 0000000000000018 A 3 1 8
[ 3] .dynstr STRTAB 0000000000000380 00000= 380
00000000000000de 0000000000000000 A 0 0 1
[ 4] .rela.dyn RELA 0= 000000000000460 00000460
00000000000000f0 0000000000000018 A 2 0 8
= [ 5] .rela.plt RELA 0000000000000550 00000550
0000000000000048 00000000= 00000018 AI 2 19 8
[ 6] .init PROGBITS 0000000000000598 00000598
00= 0000000000000d 0000000000000000 AX 0 0 1
[ 7] .plt PROGBITS 00000000000= 005b0 000005b0
0000000000000040 0000000000000010 AX 0 0 16
[ 8] .pl= t.got PROGBITS 00000000000005f0 000005f0
0000000000000018 0000000000000= 000 AX 0 0 8
[ 9] .text PROGBITS 0000000000000610 00000610
00000000= 00000497 0000000000000000 AX 0 0 16
[10] .fini PROGBITS 0000000000000aa= 7 00000aa7
0000000000000008 0000000000000000 AX 0 0 1
[11] .rodata = PROGBITS 0000000000000aaf 00000aaf
0000000000000004 0000000000000001 AM= S 0 0 1
[12] .eh_frame_hdr PROGBITS 0000000000000ab4 00000ab4
00000= 0000000001c 0000000000000000 A 0 0 4
[13] .eh_frame PROGBITS 0000000000= 000ad0 00000ad0
0000000000000064 0000000000000000 A 0 0 8
[14] .cto= rs PROGBITS 0000000000200e40 00000e40
0000000000000010 0000000000000000= WA 0 0 8
[15] .dtors PROGBITS 0000000000200e50 00000e50
0000000000= 000010 0000000000000000 WA 0 0 8
[16] .jcr PROGBITS 0000000000200e60 00= 000e60
0000000000000008 0000000000000000 WA 0 0 8
[17] .dynamic DYN= AMIC 0000000000200e68 00000e68
0000000000000150 0000000000000010 WA 3 0= 8
[18] .got PROGBITS 0000000000200fb8 00000fb8
0000000000000048 00= 00000000000008 WA 0 0 8
[19] .got.plt PROGBITS 0000000000201000 0000100= 0
0000000000000030 0000000000000008 WA 0 0 8
[20] .data PROGBITS 00= 00000000201030 00001030
0000000000000008 0000000000000000 WA 0 0 8
= [21] .bss NOBITS 0000000000201040 00001038
0000000000000050 00000000000= 00000 WA 0 0 32
[22] .comment PROGBITS 0000000000000000 00001038
00= 0000000000003c 0000000000000001 MS 0 0 1
[23] .shstrtab STRTAB 00000000= 00000000 000018fb
00000000000000b9 0000000000000000 0 0 1
[24] .sym= tab SYMTAB 0000000000000000 00001078
0000000000000648 0000000000000018 = 25 51 8
[25] .strtab STRTAB 0000000000000000 000016c0
0000000000000= 23b 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (ex= ecute), M (merge), S (strings), I (info),
L (link order), O (extra OS p= rocessing required), G (group), T (TLS),
C (compressed), x (unknown), o= (OS specific), E (exclude),
l (large), p (processor specific)

P= rogram Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags = Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
= 0x0000000000000b34 0x0000000000000b34 R E 200000
LOAD 0x0000000000000e= 40 0x0000000000200e40 0x0000000000200e40
0x00000000000001f8 0x000000000= 0000250 RW 200000
DYNAMIC 0x0000000000000e68 0x0000000000200e68 0x00000= 00000200e68
0x0000000000000150 0x0000000000000150 RW 8
GNU_EH_FRAME= 0x0000000000000ab4 0x0000000000000ab4 0x0000000000000ab4
0x00000000000= 0001c 0x000000000000001c R 4
GNU_STACK 0x0000000000000000 0x00000000000= 00000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 10 GNU_RELRO 0x0000000000000e40 0x0000000000200e40 0x0000000000200e40
0x= 00000000000001c0 0x00000000000001c0 R 1

Section to Segment mapping:=
Segment Sections...
00 .hash .dynsym .dynstr .rela.dyn .rela.plt .= init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame
01 .cto= rs .dtors .jcr .dynamic .got .got.plt .data .bss
02 .dynamic
03 .= eh_frame_hdr
04
05 .ctors .dtors .jcr .dynamic .got



From: "dalias" &l= t;dalias@libc.org>
To: "musl" <musl@lists.openwall.com><= br>Sent: Sunday, March 5, 2017 2:44:03 AM
Subject: Re: [mu= sl] Static PIE with musl and clang

On Sat, Mar 04, 2017 at 07:47:31PM -0600, Paul Sturm w= rote:
> Has anyone managed to build static PIE with musl and clang,> preferably linking with lld?
>
> I see that this has be= en done using GCC and rcrt1.o, but I cannot
> find a combination of c= ommand line parameters that will successfully
> link and execute with= clang.
>
> I am using clang 4.0.0RC2 and musl 1.1.16. I have = also tried to use
> binutils ld instead of llvm lld to no avail.
<= br>I doubt lld can do it yet; you need some equivalent of the
--no-dynam= ic-linker option I added to BFD ld. It should be possible
with clang usi= ng binutils/BFD ld. You might also be able to hack it by
using -shared -= Bsymbolic and explicitly linking rcrt1.o on the link
command line (essen= tially doing the same thing you'd do to make an
executable shared librar= y) but there are corner-case things that break
when you try to do it thi= s way. The right approach would be adding
--no-dynamic-linker to lld and= teaching clang to pass it when linking
with -static -pie.

Rich
------=_Part_2625_477381364.1488685284590-- From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11103 Path: news.gmane.org!.POSTED!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: Static PIE with musl and clang Date: Sun, 5 Mar 2017 13:21:18 +0100 Message-ID: <20170305122118.GG2082@port70.net> References: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> <20170305024403.GK1520@brightrain.aerifal.cx> <640524050.2626.1488685284594.JavaMail.zimbra@computervoice.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1488716500 11445 195.159.176.226 (5 Mar 2017 12:21:40 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 5 Mar 2017 12:21:40 +0000 (UTC) User-Agent: Mutt/1.6.0 (2016-04-01) To: musl@lists.openwall.com Original-X-From: musl-return-11118-gllmg-musl=m.gmane.org@lists.openwall.com Sun Mar 05 13:21:35 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ckVAG-00023g-0G for gllmg-musl@m.gmane.org; Sun, 05 Mar 2017 13:21:28 +0100 Original-Received: (qmail 9773 invoked by uid 550); 5 Mar 2017 12:21:30 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 9746 invoked from network); 5 Mar 2017 12:21:29 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <640524050.2626.1488685284594.JavaMail.zimbra@computervoice.com> Xref: news.gmane.org gmane.linux.lib.musl.general:11103 Archived-At: * Paul Sturm [2017-03-04 21:41:24 -0600]: > clang /usr/lib/rcrt1.o ../aslr.c -o aslr -fPIE -shared -Bsymbolic -Wl,--no-dynamic-linker > > ./aslr seg faults when I run it. you did not static link the libc at all From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11322 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Sturm Newsgroups: gmane.linux.lib.musl.general Subject: Re: Static PIE with musl and clang Date: Mon, 15 May 2017 14:24:58 -0500 (CDT) Message-ID: <1432352279.548091.1494876298089.JavaMail.zimbra@computervoice.com> References: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> <20170305024403.GK1520@brightrain.aerifal.cx> <640524050.2626.1488685284594.JavaMail.zimbra@computervoice.com> <20170305122118.GG2082@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_548090_1624246684.1494876298080" X-Trace: blaine.gmane.org 1494876320 11856 195.159.176.226 (15 May 2017 19:25:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 15 May 2017 19:25:20 +0000 (UTC) To: musl Original-X-From: musl-return-11337-gllmg-musl=m.gmane.org@lists.openwall.com Mon May 15 21:25:16 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dALcI-0002wJ-8x for gllmg-musl@m.gmane.org; Mon, 15 May 2017 21:25:15 +0200 Original-Received: (qmail 7585 invoked by uid 550); 15 May 2017 19:25:16 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 7567 invoked from network); 15 May 2017 19:25:15 -0000 X-Virus-Scanned: amavisd-new at server03.computervoice.com In-Reply-To: <20170305122118.GG2082@port70.net> X-Mailer: Zimbra 8.6.0_GA_1153 (ZimbraWebClient - FF47 (Linux)/8.6.0_GA_1153) Thread-Topic: Static PIE with musl and clang Thread-Index: zYi1PcD7ACRZJAF6A78LOfMzAXOotA== Xref: news.gmane.org gmane.linux.lib.musl.general:11322 Archived-At: ------=_Part_548090_1624246684.1494876298080 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit I decided to take a step back and try to make this work first with gcc (7.1) and ld (2.28). Both tools were built statically and musl (1.16+) is also built as a static libc.a. Test program is aslr.c: #include int main() { printf("%p\n", main); return 0; } gcc aslr.c -fPIE -pie -static-libgcc -Wl,-static -Wl,--no-dynamic-linker ./a.out Segementation fault bash-4.4# readelf -e a.out ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x300 Start of program headers: 64 (bytes into file) Start of section headers: 31560 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 6 Size of section headers: 64 (bytes) Number of section headers: 25 Section header string table index: 24 Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .hash HASH 0000000000000190 00000190 0000000000000014 0000000000000004 A 2 0 8 [ 2] .dynsym DYNSYM 00000000000001a8 000001a8 0000000000000030 0000000000000018 A 3 1 8 [ 3] .dynstr STRTAB 00000000000001d8 000001d8 0000000000000007 0000000000000000 A 0 0 1 [ 4] .rela.dyn RELA 00000000000001e0 000001e0 00000000000000c0 0000000000000018 A 2 0 8 [ 5] .init PROGBITS 00000000000002a0 000002a0 000000000000000d 0000000000000000 AX 0 0 1 [ 6] .plt PROGBITS 00000000000002b0 000002b0 0000000000000010 0000000000000010 AX 0 0 16 [ 7] .plt.got PROGBITS 00000000000002c0 000002c0 0000000000000018 0000000000000000 AX 0 0 8 [ 8] .text PROGBITS 00000000000002e0 000002e0 0000000000004f44 0000000000000000 AX 0 0 16 [ 9] .fini PROGBITS 0000000000005224 00005224 0000000000000008 0000000000000000 AX 0 0 1 [10] .rodata PROGBITS 0000000000005240 00005240 0000000000000b90 0000000000000000 A 0 0 32 [11] .eh_frame_hdr PROGBITS 0000000000005dd0 00005dd0 0000000000000024 0000000000000000 A 0 0 4 [12] .eh_frame PROGBITS 0000000000005df8 00005df8 000000000000007c 0000000000000000 A 0 0 8 [13] .ctors PROGBITS 0000000000205e88 00005e88 0000000000000010 0000000000000000 WA 0 0 8 [14] .dtors PROGBITS 0000000000205e98 00005e98 0000000000000010 0000000000000000 WA 0 0 8 [15] .data.rel.ro PROGBITS 0000000000205ea8 00005ea8 0000000000000008 0000000000000000 WA 0 0 8 [16] .dynamic DYNAMIC 0000000000205eb0 00005eb0 0000000000000130 0000000000000010 WA 3 0 8 [17] .got PROGBITS 0000000000205fe0 00005fe0 0000000000000020 0000000000000008 WA 0 0 8 [18] .got.plt PROGBITS 0000000000206000 00006000 0000000000000018 0000000000000008 WA 0 0 8 [19] .data PROGBITS 0000000000206018 00006018 00000000000000f8 0000000000000000 WA 0 0 8 [20] .bss NOBITS 0000000000206120 00006110 00000000000006e0 0000000000000000 WA 0 0 32 [21] .comment PROGBITS 0000000000000000 00006110 000000000000003e 0000000000000001 MS 0 0 1 [22] .symtab SYMTAB 0000000000000000 00006150 00000000000010e0 0000000000000018 23 100 8 [23] .strtab STRTAB 0000000000000000 00007230 000000000000085b 0000000000000000 0 0 1 [24] .shstrtab STRTAB 0000000000000000 00007a8b 00000000000000b7 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), l (large), p (processor specific) Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000005e74 0x0000000000005e74 R E 0x200000 LOAD 0x0000000000005e88 0x0000000000205e88 0x0000000000205e88 0x0000000000000288 0x0000000000000978 RW 0x200000 DYNAMIC 0x0000000000005eb0 0x0000000000205eb0 0x0000000000205eb0 0x0000000000000130 0x0000000000000130 RW 0x8 GNU_EH_FRAME 0x0000000000005dd0 0x0000000000005dd0 0x0000000000005dd0 0x0000000000000024 0x0000000000000024 R 0x4 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 0x10 GNU_RELRO 0x0000000000005e88 0x0000000000205e88 0x0000000000205e88 0x0000000000000178 0x0000000000000178 R 0x1 Section to Segment mapping: Segment Sections... 00 .hash .dynsym .dynstr .rela.dyn .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame 01 .ctors .dtors .data.rel.ro .dynamic .got .got.plt .data .bss 02 .dynamic 03 .eh_frame_hdr 04 05 .ctors .dtors .data.rel.ro .dynamic .got bash-4.4# bash-4.4# readelf -d a.out Dynamic section at offset 0x5eb0 contains 15 entries: Tag Type Name/Value 0x000000000000000c (INIT) 0x2a0 0x000000000000000d (FINI) 0x5224 0x0000000000000004 (HASH) 0x190 0x0000000000000005 (STRTAB) 0x1d8 0x0000000000000006 (SYMTAB) 0x1a8 0x000000000000000a (STRSZ) 7 (bytes) 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000015 (DEBUG) 0x0 0x0000000000000003 (PLTGOT) 0x206000 0x0000000000000007 (RELA) 0x1e0 0x0000000000000008 (RELASZ) 192 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) 0x000000006ffffffb (FLAGS_1) Flags: PIE 0x000000006ffffff9 (RELACOUNT) 8 0x0000000000000000 (NULL) 0x0 From: "Szabolcs Nagy" To: "musl" Sent: Sunday, March 5, 2017 12:21:18 PM Subject: Re: [musl] Static PIE with musl and clang * Paul Sturm [2017-03-04 21:41:24 -0600]: > clang /usr/lib/rcrt1.o ../aslr.c -o aslr -fPIE -shared -Bsymbolic -Wl,--no-dynamic-linker > ). > ./aslr seg faults when I run it. you did not static link the libc at all ------=_Part_548090_1624246684.1494876298080 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
I decided to take a step back and try to ma= ke this work first with gcc (7.1) and ld (2.28). Both tools were built stat= ically and musl (1.16+) is also built as a static libc.a.
Test program is aslr.c:

#include <std= io.h>

int main() {
  printf("%p\n", main);
  retu= rn 0;
}

gcc aslr.c -fPIE -pie -static= -libgcc -Wl,-static -Wl,--no-dynamic-linker

./a.out
Segementation= fault

bash-4.4# readelf -e a.out
ELF Header:
  Magic:   7= f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:  = ;            &n= bsp;            = ;  ELF64
  Data:       &nbs= p;            &= nbsp;         2's complement, littl= e endian
  Version:        =             &nb= sp;      1 (current)
  OS/ABI:  =             &nb= sp;            = UNIX - System V
  ABI Version:      =             &nb= sp;    0
  Type:      =             &nb= sp;           DYN (Shared= object file)
  Machine:       &= nbsp;           &nbs= p;       Advanced Micro Devices X86-64
&nb= sp; Version:          &nb= sp;            =     0x1
  Entry point address:   &nbs= p;           0x300
&nb= sp; Start of program headers:       &nbs= p;  64 (bytes into file)
  Start of section headers: &nbs= p;        31560 (bytes into file)
&nb= sp; Flags:           = ;            &n= bsp;     0x0
  Size of this header:  =              64= (bytes)
  Size of program headers:     &n= bsp;     56 (bytes)
  Number of program headers= :         6
  Size of secti= on headers:           64 = (bytes)
  Number of section headers:     &= nbsp;   25
  Section header string table index: 24
Section Headers:
  [Nr] Name      &n= bsp;       Type     =         Address    &= nbsp;      Offset
     = ;  Size          &nb= sp;   EntSize        &nbs= p; Flags  Link  Info  Align
  [ 0]   =             &nb= sp;   NULL         &= nbsp;   0000000000000000  00000000
   &nbs= p;   0000000000000000  0000000000000000   &nb= sp;       0     0 &n= bsp;   0
  [ 1] .hash      =        HASH     &nbs= p;       0000000000000190  00000190
&= nbsp;      0000000000000014  0000000000000004=    A       2    = ; 0     8
  [ 2] .dynsym    = ;       DYNSYM     &= nbsp;     00000000000001a8  000001a8
 &nbs= p;     0000000000000030  0000000000000018 &nb= sp; A       3     1 =     8
  [ 3] .dynstr     &n= bsp;     STRTAB      &nbs= p;    00000000000001d8  000001d8
   &= nbsp;   0000000000000007  0000000000000000   A&nbs= p;      0     0  &nb= sp;  1
  [ 4] .rela.dyn      &nb= sp;  RELA          &= nbsp;  00000000000001e0  000001e0
    &nbs= p;  00000000000000c0  0000000000000018   A  &= nbsp;    2     0    = 8
  [ 5] .init        &nbs= p;    PROGBITS       &nbs= p; 00000000000002a0  000002a0
       = 000000000000000d  0000000000000000  AX    &nb= sp;  0     0     1
  [= 6] .plt           &= nbsp;  PROGBITS         000000= 00000002b0  000002b0
       000000000= 0000010  0000000000000010  AX      = 0     0     16
  [ 7] .plt= .got          PROGBITS &n= bsp;       00000000000002c0  000002c0       0000000000000018  00000000000000= 00  AX       0     0=      8
  [ 8] .text    &nbs= p;        PROGBITS   &nbs= p;     00000000000002e0  000002e0
  &= nbsp;    0000000000004f44  0000000000000000  AX&nb= sp;      0     0  &n= bsp;  16
  [ 9] .fini       = ;      PROGBITS      = ;   0000000000005224  00005224
    &n= bsp;  0000000000000008  0000000000000000  AX  &nbs= p;    0     0     1<= br>  [10] .rodata         = ;  PROGBITS         0000000000= 005240  00005240
       0000000000000= b90  0000000000000000   A      = ; 0     0     32
  [11] .eh= _frame_hdr     PROGBITS     &n= bsp;   0000000000005dd0  00005dd0
    = ;   0000000000000024  0000000000000000   A &n= bsp;     0     0   &= nbsp; 4
  [12] .eh_frame       &= nbsp; PROGBITS         000000000000= 5df8  00005df8
       000000000000007= c  0000000000000000   A       = 0     0     8
  [13] .ctors=             PROGBITS=          0000000000205e88  000= 05e88
       0000000000000010  000000= 0000000000  WA       0   =   0     8
  [14] .dtors   &= nbsp;        PROGBITS   &= nbsp;     0000000000205e98  00005e98
 &nbs= p;     0000000000000010  0000000000000000  WA=        0     0  = ;   8
  [15] .data.rel.ro      P= ROGBITS         0000000000205ea8&nb= sp; 00005ea8
       0000000000000008 = 0000000000000000  WA       0  = ;   0     8
  [16] .dynamic &nbs= p;        DYNAMIC    = ;      0000000000205eb0  00005eb0
 &n= bsp;     0000000000000130  0000000000000010  = WA       3     0 &nb= sp;   8
  [17] .got      &n= bsp;       PROGBITS    &n= bsp;    0000000000205fe0  00005fe0
   = ;    0000000000000020  0000000000000008  WA &= nbsp;     0     0   =   8
  [18] .got.plt       &= nbsp;  PROGBITS         000000= 0000206000  00006000
       000000000= 0000018  0000000000000008  WA      = 0     0     8
  [19] .data=              PR= OGBITS         0000000000206018&nbs= p; 00006018
       00000000000000f8  = 0000000000000000  WA       0  =    0     8
  [20] .bss  &nb= sp;           NOBITS = ;          0000000000206120&nb= sp; 00006110
       00000000000006e0 = 0000000000000000  WA       0  = ;   0     32
  [21] .comment &nb= sp;        PROGBITS   &nb= sp;     0000000000000000  00006110
  =      000000000000003e  0000000000000001  MS&n= bsp;      0     0  &= nbsp;  1
  [22] .symtab      &nb= sp;    SYMTAB        = ;   0000000000000000  00006150
    &n= bsp;  00000000000010e0  0000000000000018    &= nbsp;     23   100     8<= br>  [23] .strtab         = ;  STRTAB           = 0000000000000000  00007230
       000= 000000000085b  0000000000000000      &nb= sp;    0     0     1=
  [24] .shstrtab         S= TRTAB           000000000= 0000000  00007a8b
       000000000000= 00b7  0000000000000000        =    0     0     1
Key t= o Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings= ), I (info),
  L (link order), O (extra OS processing required), G = (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E= (exclude),
  l (large), p (processor specific)

Program Head= ers:
  Type         &n= bsp; Offset          &nbs= p;  VirtAddr         &nbs= p; PhysAddr
          =        FileSiz     &= nbsp;      MemSiz     &nb= sp;        Flags  Align
  L= OAD           0x000000000= 0000000 0x0000000000000000 0x0000000000000000
    &n= bsp;            0x00= 00000000005e74 0x0000000000005e74  R E    0x200000
&= nbsp; LOAD           0x00= 00000000005e88 0x0000000000205e88 0x0000000000205e88
   &= nbsp;           &nbs= p; 0x0000000000000288 0x0000000000000978  RW     0= x200000
  DYNAMIC        0x00000= 00000005eb0 0x0000000000205eb0 0x0000000000205eb0
   &nbs= p;             = 0x0000000000000130 0x0000000000000130  RW     0x8<= br>  GNU_EH_FRAME   0x0000000000005dd0 0x0000000000005dd0 0x= 0000000000005dd0
         &= nbsp;       0x0000000000000024 0x000000000000= 0024  R      0x4
  GNU_STACK &nb= sp;    0x0000000000000000 0x0000000000000000 0x0000000000000= 000
           &n= bsp;     0x0000000000000000 0x0000000000000000  RW=      0x10
  GNU_RELRO    &n= bsp; 0x0000000000005e88 0x0000000000205e88 0x0000000000205e88
 &nbs= p;            &= nbsp;  0x0000000000000178 0x0000000000000178  R   =    0x1

 Section to Segment mapping:
  Segment= Sections...
   00     .hash .dynsym .dyns= tr .rela.dyn .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_fram= e
   01     .ctors .dtors .data.rel.ro .d= ynamic .got .got.plt .data .bss
   02    = .dynamic
   03     .eh_frame_hdr
&nb= sp;  04     
   05   &= nbsp; .ctors .dtors .data.rel.ro .dynamic .got
bash-4.4#

bash-4.4# readelf -d a.out

Dyna= mic section at offset 0x5eb0 contains 15 entries:
  Tag  =       Type      &nbs= p;            &= nbsp;     Name/Value
 0x000000000000000c (INIT)=             &nb= sp;  0x2a0
 0x000000000000000d (FINI)    &= nbsp;          0x5224
 = ;0x0000000000000004 (HASH)        &= nbsp;      0x190
 0x0000000000000005 (STRT= AB)            = 0x1d8
 0x0000000000000006 (SYMTAB)     &n= bsp;       0x1a8
 0x000000000000000a = (STRSZ)           &n= bsp;  7 (bytes)
 0x000000000000000b (SYMENT)   =           24 (bytes)
 = 0x0000000000000015 (DEBUG)        &= nbsp;     0x0
 0x0000000000000003 (PLTGOT) = ;            0x20600= 0
 0x0000000000000007 (RELA)      &nb= sp;        0x1e0
 0x000000000000= 0008 (RELASZ)          &n= bsp;  192 (bytes)
 0x0000000000000009 (RELAENT)  &nb= sp;         24 (bytes)
 0x0= 00000006ffffffb (FLAGS_1)        &n= bsp;   Flags: PIE
 0x000000006ffffff9 (RELACOUNT) &n= bsp;        8
 0x000000000000000= 0 (NULL)           &= nbsp;   0x0


From: "Szabolcs Na= gy" <nsz@port70.net>
To: "musl" <musl@lists.openwall.com= >
Sent: Sunday, March 5, 2017 12:21:18 PM
Subject: R= e: [musl] Static PIE with musl and clang

* Paul Sturm <psturm@computervoice.com> [2= 017-03-04 21:41:24 -0600]:
> clang /usr/lib/rcrt1.o ../aslr.c -o aslr= -fPIE -shared -Bsymbolic -Wl,--no-dynamic-linker
> ).
> ./as= lr seg faults when I run it.

you did not static link the libc at al= l
------=_Part_548090_1624246684.1494876298080-- From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11323 Path: news.gmane.org!.POSTED!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: Static PIE with musl and clang Date: Tue, 16 May 2017 11:02:09 +0200 Message-ID: <20170516090209.GC9350@port70.net> References: <1450280459.2314.1488678451062.JavaMail.zimbra@computervoice.com> <20170305024403.GK1520@brightrain.aerifal.cx> <640524050.2626.1488685284594.JavaMail.zimbra@computervoice.com> <20170305122118.GG2082@port70.net> <1432352279.548091.1494876298089.JavaMail.zimbra@computervoice.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1494925345 12739 195.159.176.226 (16 May 2017 09:02:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 16 May 2017 09:02:25 +0000 (UTC) User-Agent: Mutt/1.6.0 (2016-04-01) To: musl@lists.openwall.com Original-X-From: musl-return-11338-gllmg-musl=m.gmane.org@lists.openwall.com Tue May 16 11:02:21 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dAYN1-0003Cp-Vu for gllmg-musl@m.gmane.org; Tue, 16 May 2017 11:02:20 +0200 Original-Received: (qmail 5569 invoked by uid 550); 16 May 2017 09:02:22 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 5547 invoked from network); 16 May 2017 09:02:22 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <1432352279.548091.1494876298089.JavaMail.zimbra@computervoice.com> Xref: news.gmane.org gmane.linux.lib.musl.general:11323 Archived-At: * Paul Sturm [2017-05-15 14:24:58 -0500]: > I decided to take a step back and try to make this work first with gcc (7.1) and ld (2.28). Both tools were built statically and musl (1.16+) is also built as a static libc.a. > > Test program is aslr.c: > > #include > > int main() { > printf("%p\n", main); > return 0; > } > > gcc aslr.c -fPIE -pie -static-libgcc -Wl,-static -Wl,--no-dynamic-linker > > ./a.out > Segementation fault there is no -static passed to the compiler so it cannot possibly select the right crt file. you need to patch gcc for static pie support, or pass all the necessary linker flags manually (including the crt files). https://github.com/richfelker/musl-cross-make/blob/master/patches/gcc-6.3.0/0010-static-pie-support.diff