From 9fccad30cf295b750976fbf0e0a43d85880d1d3c Mon Sep 17 00:00:00 2001 From: miod Date: Tue, 30 Dec 2014 19:26:38 +0000 Subject: [PATCH] self-relocating crt0 bits for sh, and enable static pie by default. With some archdep.h help from kettenis@ --- gnu/gcc/gcc/config/sh/openbsd.h | 3 +- .../binutils/ld/emulparams/shelf_obsd.sh | 2 +- lib/csu/sh/md_init.h | 73 +++++++++++++++++-- libexec/ld.so/sh/archdep.h | 4 +- share/mk/bsd.own.mk | 4 +- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/gnu/gcc/gcc/config/sh/openbsd.h b/gnu/gcc/gcc/config/sh/openbsd.h index 20521a79c1e..cb665a2dd3d 100644 --- a/gnu/gcc/gcc/config/sh/openbsd.h +++ b/gnu/gcc/gcc/config/sh/openbsd.h @@ -90,7 +90,8 @@ Boston, MA 02110-1301, USA. */ /* As an elf system, we need crtbegin/crtend stuff. */ #undef STARTFILE_SPEC #define STARTFILE_SPEC "\ - %{!shared: %{pg:gcrt0%O%s} %{!pg:%{p:gcrt0%O%s} %{!p:crt0%O%s}} \ + %{!shared: %{pg:gcrt0%O%s} %{!pg:%{p:gcrt0%O%s} \ + %{!p:%{!static:crt0%O%s} %{static:%{pie:rcrt0%O%s} %{!pie:crt0%O%s}}}} \ crtbegin%O%s} %{shared:crtbeginS%O%s}" #undef ENDFILE_SPEC #define ENDFILE_SPEC "%{!shared:crtend%O%s} %{shared:crtendS%O%s}" diff --git a/gnu/usr.bin/binutils/ld/emulparams/shelf_obsd.sh b/gnu/usr.bin/binutils/ld/emulparams/shelf_obsd.sh index 03883ef4ab6..2fa0f7157dc 100644 --- a/gnu/usr.bin/binutils/ld/emulparams/shelf_obsd.sh +++ b/gnu/usr.bin/binutils/ld/emulparams/shelf_obsd.sh @@ -10,7 +10,7 @@ MAXPAGESIZE=0x10000 DATA_START_SYMBOLS='__data_start = . ;'; -ENTRY=_start +ENTRY=__start unset EMBEDDED unset OTHER_SECTIONS diff --git a/lib/csu/sh/md_init.h b/lib/csu/sh/md_init.h index 6fb408d3871..7a1b5d971b2 100644 --- a/lib/csu/sh/md_init.h +++ b/lib/csu/sh/md_init.h @@ -1,4 +1,4 @@ -/* $OpenBSD: md_init.h,v 1.3 2014/04/18 15:09:52 guenther Exp $ */ +/* $OpenBSD: md_init.h,v 1.4 2014/12/30 19:26:38 miod Exp $ */ /* $NetBSD: dot_init.h,v 1.3 2005/12/24 22:02:10 perry Exp $ */ /*- @@ -68,15 +68,12 @@ __asm(".section " #section "\n" \ "2: .previous"); -/* no ASM stub for __start; the C routine can be called directly */ -#define MD_START __start - #if defined(__SH4__) && !defined(__SH4_NOFPU__) #include -#define MD_CRT0_START \ +#define MD_FPU_START \ void __set_fpscr(unsigned int); \ - unsigned int __fpscr_values[2] + unsigned int __fpscr_values[2]; #define MD_START_SETUP \ __set_fpscr(0); \ @@ -84,5 +81,69 @@ __asm(".section " #section "\n" \ __fpscr_values[1] |= FPSCR_DN; \ __asm volatile ("lds %0, fpscr" \ : : "r" (__fpscr_values[1])); +#else +#define MD_FPU_START #endif /* defined(__SH4__) && !defined(__SH4_NOFPU__) */ +#define MD_CRT0_START \ + MD_FPU_START \ + __asm (".globl __start; __start = ___start") + +#define MD_RCRT0_START \ + MD_FPU_START \ + __asm ( \ + ".text \n" \ + " .align 2 \n" \ + " .globl __start \n" \ + " .type __start,@function \n" \ + "__start: \n" \ + " mov r15, r12 \n" \ + " mov r15, r4 \n" \ + " add #-72, r15 \n" \ + " mov r15, r5 \n" \ + " bsr 1f \n" \ + " nop \n" \ + "1: \n" \ + ".L_offbase: \n" \ + " sts pr, r0 \n" \ + " mov.l .L_dynamic, r6 \n" \ + " add r0, r6 \n" \ + " mov.l .L_boot_bind, r0 \n" \ + " bsrf r0 \n" \ + " nop \n" \ + ".L_call_boot_bind: \n" \ + " mov r12, r15 \n" \ + " mov.l @r15, r4 /* argc */ \n" \ + " mov r15, r5 \n" \ + " add #4, r5 /* argv */ \n" \ + " mov r4, r6 \n" \ + " add #1, r6 \n" \ + " shll2 r6 \n" \ + " add r5, r6 /* envp */ \n" \ + " bra ___start \n" \ + " mov #0, r7 /* cleanup */ \n" \ + " .align 2 \n" \ + ".L_boot_bind: \n" \ + " .long _dl_boot_bind - .L_call_boot_bind \n" \ + ".L_datasize: \n" \ + " .long 4 + 4 + (16 * 4) \n" \ + ".L_dynamic: \n" \ + " .long _DYNAMIC - .L_offbase \n" \ + \ + " .align 2 \n" \ + " .globl _dl_printf \n" \ + " .type _dl_printf,@function \n" \ + "_dl_printf: \n" \ + " rts \n" \ + " nop \n" \ + \ + " .align 2 \n" \ + " .globl _dl_exit \n" \ + " .type _dl_exit,@function \n" \ + "_dl_exit: \n" \ + " mov #1, r0 \n" \ + " .word 0xc380 /* trapa #0x80 */ \n" \ + ".previous") + +/* no ASM stub for __start; the C routine can be called directly */ +#define MD_START ___start diff --git a/libexec/ld.so/sh/archdep.h b/libexec/ld.so/sh/archdep.h index 8da20ac5a43..00f8aaf87ea 100644 --- a/libexec/ld.so/sh/archdep.h +++ b/libexec/ld.so/sh/archdep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: archdep.h,v 1.3 2014/07/05 12:22:41 miod Exp $ */ +/* $OpenBSD: archdep.h,v 1.4 2014/12/30 19:26:38 miod Exp $ */ /* * Copyright (c) 1998 Per Fogelstrom, Opsycon AB @@ -74,6 +74,8 @@ RELOC_RELA(Elf32_Rela *r, const Elf32_Sym *s, Elf32_Addr *p, unsigned long v, { if (ELF_R_TYPE(r->r_info) == R_SH_RELATIVE) { *p = v + r->r_addend; + } else if (ELF_R_TYPE(r->r_info) == R_SH_DIR32) { + *p = s->st_value + v + r->r_addend; } else { /* XXX - printf might not work here, but we give it a shot. */ _dl_printf("Unknown bootstrap relocation.\n"); diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk index 04bac25b3ec..fb78b9eb3a9 100644 --- a/share/mk/bsd.own.mk +++ b/share/mk/bsd.own.mk @@ -1,4 +1,4 @@ -# $OpenBSD: bsd.own.mk,v 1.156 2014/12/28 05:17:21 deraadt Exp $ +# $OpenBSD: bsd.own.mk,v 1.157 2014/12/30 19:26:38 miod Exp $ # $NetBSD: bsd.own.mk,v 1.24 1996/04/13 02:08:09 thorpej Exp $ # Host-specific overrides @@ -25,7 +25,7 @@ BINUTILS217_ARCH=hppa64 ia64 # m88k unknown # hppa64 unknown PIE_ARCH=alpha amd64 hppa i386 mips64 mips64el powerpc sh sparc64 -STATICPIE_ARCH=alpha amd64 hppa i386 powerpc sparc64 +STATICPIE_ARCH=alpha amd64 hppa i386 powerpc sh sparc64 .for _arch in ${MACHINE_ARCH} .if !empty(GCC3_ARCH:M${_arch}) -- 2.20.1