mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] V2 Enable linking to a static position independent executable
@ 2020-04-06 18:07 harald
  2020-04-06 22:04 ` Fangrui Song
  0 siblings, 1 reply; 10+ messages in thread
From: harald @ 2020-04-06 18:07 UTC (permalink / raw)
  To: musl; +Cc: Harald Hoyer

From: Harald Hoyer <harald@redhat.com>

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 <<EOF
 -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
 
 *link_libgcc:
 -L$libdir -L .%s
@@ -17,13 +17,13 @@ cat <<EOF
 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] V2 Enable linking to a static position independent executable
  2020-04-06 18:07 [musl] [PATCH] V2 Enable linking to a static position independent executable harald
@ 2020-04-06 22:04 ` Fangrui Song
  2020-04-07  8:42   ` Szabolcs Nagy
  2020-04-07 13:17   ` [musl] [PATCH] " harald
  0 siblings, 2 replies; 10+ messages in thread
From: Fangrui Song @ 2020-04-06 22:04 UTC (permalink / raw)
  To: harald; +Cc: musl

On 2020-04-06, harald@redhat.com wrote:
>From: Harald Hoyer <harald@redhat.com>
>
>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 <<EOF
> -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 <<EOF
> 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
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] V2 Enable linking to a static position independent executable
  2020-04-06 22:04 ` Fangrui Song
@ 2020-04-07  8:42   ` Szabolcs Nagy
  2020-04-07 13:17   ` [musl] [PATCH] " harald
  1 sibling, 0 replies; 10+ messages in thread
From: Szabolcs Nagy @ 2020-04-07  8:42 UTC (permalink / raw)
  To: musl; +Cc: harald, Fangrui Song

* Fangrui Song <i@maskray.me> [2020-04-06 15:04:03 -0700]:
> On 2020-04-06, harald@redhat.com wrote:
> > 
> > *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).

yes.

the spec file cannot be reliable because the default
toolchain config is not visible to the spec file
logic, so for non-basic use we recommend to build a
musl based cross toolchain.

> 
> (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)

because it's easy to get text relocs by mistake
(compiling without -fPIE but linking static pie)
and text relocs are always wrong for static pie
(neither glibc nor musl plans to support them).

> > *link_libgcc:
> > -L$libdir -L .%s
> > @@ -17,13 +17,13 @@ cat <<EOF
> > 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
> > 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-06 22:04 ` Fangrui Song
  2020-04-07  8:42   ` Szabolcs Nagy
@ 2020-04-07 13:17   ` harald
  2020-04-23 11:59     ` Harald Hoyer
  2020-04-24 15:08     ` Rich Felker
  1 sibling, 2 replies; 10+ messages in thread
From: harald @ 2020-04-07 13:17 UTC (permalink / raw)
  To: musl; +Cc: Harald Hoyer

From: Harald Hoyer <harald@redhat.com>

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 | 6 +++---
 1 file changed, 3 insertions(+), 3 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..4d93626a
--- a/tools/musl-gcc.specs.sh
+++ b/tools/musl-gcc.specs.sh
@@ -17,13 +17,13 @@ cat <<EOF
 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-07 13:17   ` [musl] [PATCH] " harald
@ 2020-04-23 11:59     ` Harald Hoyer
  2020-04-24 15:08     ` Rich Felker
  1 sibling, 0 replies; 10+ messages in thread
From: Harald Hoyer @ 2020-04-23 11:59 UTC (permalink / raw)
  To: musl

Hello,

currently this patch version is in Fedora's musl-gcc rpm and seems to work just fine.

Any more changes needed? Any concerns?

Do I need to do additional tasks to submit that as a PR?

Stay healthy!

Harald


Am 07.04.20 um 15:17 schrieb harald@redhat.com:
> From: Harald Hoyer <harald@redhat.com>
> 
> 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 | 6 +++---
>  1 file changed, 3 insertions(+), 3 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..4d93626a
> --- a/tools/musl-gcc.specs.sh
> +++ b/tools/musl-gcc.specs.sh
> @@ -17,13 +17,13 @@ cat <<EOF
>  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:
>  
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-07 13:17   ` [musl] [PATCH] " harald
  2020-04-23 11:59     ` Harald Hoyer
@ 2020-04-24 15:08     ` Rich Felker
  2020-04-27 12:24       ` Harald Hoyer
  1 sibling, 1 reply; 10+ messages in thread
From: Rich Felker @ 2020-04-24 15:08 UTC (permalink / raw)
  To: harald; +Cc: musl

On Tue, Apr 07, 2020 at 03:17:07PM +0200, harald@redhat.com wrote:
> diff --git a/tools/musl-gcc.specs.sh b/tools/musl-gcc.specs.sh
> old mode 100644
> new mode 100755
> index 30492574..4d93626a
> --- a/tools/musl-gcc.specs.sh
> +++ b/tools/musl-gcc.specs.sh
> @@ -17,13 +17,13 @@ cat <<EOF
>  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

Is there a reason for this change? I think crtendS.o is always
preferable and the other ones just exist for weird historical reasons.

>  *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}}

There are multiple unrelated and probably unwanted changes here:

* --build-id?
* --eh-frame-header but only for non-static?
* --hash-style=gnu?
* Disallowing -rdynamic for static[-pie]?
* ... ?

I think all that should be done here is ensuring that
--no-dynamic-linker is passed whenever -static-pie is. Is there
something else that's a wanted change that I'm overlooking?

Rich

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-24 15:08     ` Rich Felker
@ 2020-04-27 12:24       ` Harald Hoyer
  2020-04-27 12:32         ` harald
  0 siblings, 1 reply; 10+ messages in thread
From: Harald Hoyer @ 2020-04-27 12:24 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

Am 24.04.20 um 17:08 schrieb Rich Felker:
> On Tue, Apr 07, 2020 at 03:17:07PM +0200, harald@redhat.com wrote:
>> diff --git a/tools/musl-gcc.specs.sh b/tools/musl-gcc.specs.sh
>> old mode 100644
>> new mode 100755
>> index 30492574..4d93626a
>> --- a/tools/musl-gcc.specs.sh
>> +++ b/tools/musl-gcc.specs.sh
>> @@ -17,13 +17,13 @@ cat <<EOF
>>  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
> 
> Is there a reason for this change? I think crtendS.o is always
> preferable and the other ones just exist for weird historical reasons.

I just didn't want to divert too much from the original gcc spec.

> 
>>  *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}}
> 

Same as above. I just didn't want to divert too much from the original gcc spec. But you are right. Small steps.

> There are multiple unrelated and probably unwanted changes here:
> 
> * --build-id> * --eh-frame-header but only for non-static?
> * --hash-style=gnu?
> * Disallowing -rdynamic for static[-pie]?
> * ... ?
> 
> I think all that should be done here is ensuring that
> --no-dynamic-linker is passed whenever -static-pie is. Is there
> something else that's a wanted change that I'm overlooking?
> 
> Rich
> 

Will update with a minimal diff.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-27 12:24       ` Harald Hoyer
@ 2020-04-27 12:32         ` harald
  2020-05-08  9:05           ` Harald Hoyer
  2020-08-25 10:52           ` Harald Hoyer
  0 siblings, 2 replies; 10+ messages in thread
From: harald @ 2020-04-27 12:32 UTC (permalink / raw)
  To: musl; +Cc: Harald Hoyer

From: Harald Hoyer <harald@redhat.com>

This also enables address space layout randomization (ASLR).

$ cat hello.c

int main()
{
  printf("main = 0x%lxd\n", main);
  return 0;
}

$ gcc -fPIE -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, with debug_info, not stripped, too many notes (256)

$ ./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 0x104f
There are 7 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000308 0x0000000000000308  R      0x1000
  LOAD           0x0000000000001000 0x0000000000001000 0x0000000000001000
                 0x0000000000003eb7 0x0000000000003eb7  R E    0x1000
  LOAD           0x0000000000005000 0x0000000000005000 0x0000000000005000
                 0x000000000000136c 0x000000000000136c  R      0x1000
  LOAD           0x0000000000006e50 0x0000000000007e50 0x0000000000007e50
                 0x00000000000002e0 0x00000000000009a0  RW     0x1000
  DYNAMIC        0x0000000000006e70 0x0000000000007e70 0x0000000000007e70
                 0x0000000000000180 0x0000000000000180  RW     0x8
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x10
  GNU_RELRO      0x0000000000006e50 0x0000000000007e50 0x0000000000007e50
                 0x00000000000001b0 0x00000000000001b0  R      0x1

 Section to Segment mapping:
  Segment Sections...
   00     .hash .gnu.hash .dynsym .dynstr .rela.dyn
   01     .init .plt .text .fini
   02     .rodata .eh_frame
   03     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .data .bss
   04     .dynamic
   05
   06     .init_array .fini_array .data.rel.ro .dynamic .got
---
 tools/musl-gcc.specs.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 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..ed584ed3
--- a/tools/musl-gcc.specs.sh
+++ b/tools/musl-gcc.specs.sh
@@ -17,13 +17,13 @@ cat <<EOF
 libgcc.a%s %:if-exists(libgcc_eh.a%s)
 
 *startfile:
-%{!shared: $libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
+%{shared:;static-pie:$libdir/rcrt1.o; :$libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
 
 *endfile:
 crtendS.o%s $libdir/crtn.o
 
 *link:
--dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{rdynamic:-export-dynamic}
+-dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{static-pie:-static -pie --no-dynamic-linker} %{rdynamic:-export-dynamic}
 
 *esp_link:
 
-- 
2.26.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-27 12:32         ` harald
@ 2020-05-08  9:05           ` Harald Hoyer
  2020-08-25 10:52           ` Harald Hoyer
  1 sibling, 0 replies; 10+ messages in thread
From: Harald Hoyer @ 2020-05-08  9:05 UTC (permalink / raw)
  To: musl, Rich Felker

I think, that is as small and non invasive, as it can get.

> diff --git a/tools/musl-gcc.specs.sh b/tools/musl-gcc.specs.sh
> old mode 100644
> new mode 100755
> index 30492574..ed584ed3
> --- a/tools/musl-gcc.specs.sh
> +++ b/tools/musl-gcc.specs.sh
> @@ -17,13 +17,13 @@ cat <<EOF
>  libgcc.a%s %:if-exists(libgcc_eh.a%s)
>  
>  *startfile:
> -%{!shared: $libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
> +%{shared:;static-pie:$libdir/rcrt1.o; :$libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
>  
>  *endfile:
>  crtendS.o%s $libdir/crtn.o
>  
>  *link:
> --dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{rdynamic:-export-dynamic}
> +-dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{static-pie:-static -pie --no-dynamic-linker} %{rdynamic:-export-dynamic}
>  
>  *esp_link:
>  
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [musl] [PATCH] Enable linking to a static position independent executable
  2020-04-27 12:32         ` harald
  2020-05-08  9:05           ` Harald Hoyer
@ 2020-08-25 10:52           ` Harald Hoyer
  1 sibling, 0 replies; 10+ messages in thread
From: Harald Hoyer @ 2020-08-25 10:52 UTC (permalink / raw)
  To: musl

Am 27.04.20 um 14:32 schrieb harald@redhat.com:
> From: Harald Hoyer <harald@redhat.com>
> 
> This also enables address space layout randomization (ASLR).
> 
> $ cat hello.c
> 
> int main()
> {
>   printf("main = 0x%lxd\n", main);
>   return 0;
> }
> 
> $ gcc -fPIE -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, with debug_info, not stripped, too many notes (256)
> 
> $ ./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 0x104f
> There are 7 program headers, starting at offset 64
> 
> Program Headers:
>   Type           Offset             VirtAddr           PhysAddr
>                  FileSiz            MemSiz              Flags  Align
>   LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000308 0x0000000000000308  R      0x1000
>   LOAD           0x0000000000001000 0x0000000000001000 0x0000000000001000
>                  0x0000000000003eb7 0x0000000000003eb7  R E    0x1000
>   LOAD           0x0000000000005000 0x0000000000005000 0x0000000000005000
>                  0x000000000000136c 0x000000000000136c  R      0x1000
>   LOAD           0x0000000000006e50 0x0000000000007e50 0x0000000000007e50
>                  0x00000000000002e0 0x00000000000009a0  RW     0x1000
>   DYNAMIC        0x0000000000006e70 0x0000000000007e70 0x0000000000007e70
>                  0x0000000000000180 0x0000000000000180  RW     0x8
>   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000  RW     0x10
>   GNU_RELRO      0x0000000000006e50 0x0000000000007e50 0x0000000000007e50
>                  0x00000000000001b0 0x00000000000001b0  R      0x1
> 
>  Section to Segment mapping:
>   Segment Sections...
>    00     .hash .gnu.hash .dynsym .dynstr .rela.dyn
>    01     .init .plt .text .fini
>    02     .rodata .eh_frame
>    03     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .data .bss
>    04     .dynamic
>    05
>    06     .init_array .fini_array .data.rel.ro .dynamic .got
> ---
>  tools/musl-gcc.specs.sh | 4 ++--
>  1 file changed, 2 insertions(+), 2 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..ed584ed3
> --- a/tools/musl-gcc.specs.sh
> +++ b/tools/musl-gcc.specs.sh
> @@ -17,13 +17,13 @@ cat <<EOF
>  libgcc.a%s %:if-exists(libgcc_eh.a%s)
>  
>  *startfile:
> -%{!shared: $libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
> +%{shared:;static-pie:$libdir/rcrt1.o; :$libdir/Scrt1.o} $libdir/crti.o crtbeginS.o%s
>  
>  *endfile:
>  crtendS.o%s $libdir/crtn.o
>  
>  *link:
> --dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{rdynamic:-export-dynamic}
> +-dynamic-linker $ldso -nostdlib %{shared:-shared} %{static:-static} %{static-pie:-static -pie --no-dynamic-linker} %{rdynamic:-export-dynamic}
>  
>  *esp_link:
>  
> 


Anythink missing? Does it need more work?


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-08-25 10:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-06 18:07 [musl] [PATCH] V2 Enable linking to a static position independent executable harald
2020-04-06 22:04 ` Fangrui Song
2020-04-07  8:42   ` Szabolcs Nagy
2020-04-07 13:17   ` [musl] [PATCH] " harald
2020-04-23 11:59     ` Harald Hoyer
2020-04-24 15:08     ` Rich Felker
2020-04-27 12:24       ` Harald Hoyer
2020-04-27 12:32         ` harald
2020-05-08  9:05           ` Harald Hoyer
2020-08-25 10:52           ` Harald Hoyer

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).