Diff below makes static pie work on arm. While reviewing the MD_CRT0_START assembly I noticed that we don't actually need to save r4 and r5 on the stack as those arguments are unused. I also removed the useless comment around the stack alignment code.
I'll probably hold off on the bsd.own.mk bit for a bit to give people the opportunity to install a proper rcrt0.o file. ok? Index: libexec/ld.so/arm/archdep.h =================================================================== RCS file: /cvs/src/libexec/ld.so/arm/archdep.h,v retrieving revision 1.7 diff -u -p -r1.7 archdep.h --- libexec/ld.so/arm/archdep.h 18 May 2016 20:40:20 -0000 1.7 +++ libexec/ld.so/arm/archdep.h 7 Sep 2016 20:22:20 -0000 @@ -60,6 +60,10 @@ RELOC_DYN(Elf_Rel *r, const Elf_Sym *s, { if (ELF_R_TYPE(r->r_info) == R_ARM_RELATIVE) { *p += v; + } else if (ELF_R_TYPE(r->r_info) == R_ARM_GLOB_DAT) { + *p += v + s->st_value; + } else if (ELF_R_TYPE(r->r_info) == R_ARM_ABS32) { + *p += v + s->st_value; } else { /* XXX - printf might not work here, but we give it a shot. */ _dl_printf("Unknown bootstrap relocation.\n"); Index: lib/csu/arm/md_init.h =================================================================== RCS file: /cvs/src/lib/csu/arm/md_init.h,v retrieving revision 1.7 diff -u -p -r1.7 md_init.h --- lib/csu/arm/md_init.h 24 Mar 2016 05:27:19 -0000 1.7 +++ lib/csu/arm/md_init.h 7 Sep 2016 20:22:20 -0000 @@ -87,21 +87,70 @@ "__start: \n" \ " mov r3, r0 /* cleanup */ \n" \ "/* Get argc/argv/envp from stack */ \n" \ - " ldr r0, [sp, #0x0000] \n" \ - " add r1, sp, #0x0004 \n" \ + " ldr r0, [sp, #0] \n" \ + " add r1, sp, #4 \n" \ " add r2, r1, r0, lsl #2 \n" \ - " add r2, r2, #0x0004 \n" \ + " add r2, r2, #4 \n" \ " \n" \ "/* \n" \ " * Ensure the stack is properly \n" \ " * aligned before calling C code. \n" \ " */ \n" \ - /* #if 1 */ \ " bic sp, sp, #7" /*__STRING(STACKALIGNBYTES)*/ " \n" \ - /* #endif */ \ " sub sp, sp, #8 \n" \ - " str r5, [sp, #4] \n" \ - " str r4, [sp, #0] \n" \ " \n" \ " b ___start \n" \ + ".previous"); + +#define MD_RCRT0_START \ + char **environ, *__progname; \ + __asm( \ + ".text \n" \ + " .align 0 \n" \ + " .globl _start \n" \ + " .globl __start \n" \ + "_start: \n" \ + "__start: \n" \ + " mov fp, sp \n" \ + " mov r0, fp \n" \ + " \n" \ + " sub sp, sp, #4+4+(16*4) \n" \ + " add r1, sp, #4 \n" \ + " \n" \ + " ldr r8, .L_GOT \n" \ + "1: add r8, pc, r8 \n" \ + " ldr r2, .L__DYNAMIC \n" \ + " add r2, r2, r8 \n" \ + " \n" \ + " bl _dl_boot_bind \n" \ + " \n" \ + " mov sp, fp \n" \ + " mov fp, #0 \n" \ + " \n" \ + " mov r3, #0 /* cleanup */ \n" \ + "/* Get argc/argv/envp from stack */ \n" \ + " ldr r0, [sp, #0] \n" \ + " add r1, sp, #4 \n" \ + " add r2, r1, r0, lsl #2 \n" \ + " add r2, r2, #4 \n" \ + " \n" \ + "/* \n" \ + " * Ensure the stack is properly \n" \ + " * aligned before calling C code. \n" \ + " */ \n" \ + " bic sp, sp, #7" /*__STRING(STACKALIGNBYTES)*/ " \n" \ + " sub sp, sp, #8 \n" \ + " \n" \ + " b ___start \n" \ + " \n" \ + ".L_GOT: \n" \ + " .long _GLOBAL_OFFSET_TABLE_-(1b+8) \n" \ + ".L__DYNAMIC: \n" \ + " .long _DYNAMIC(GOTOFF) \n" \ + " \n" \ + "_dl_exit: \n" \ + " mov r12, #1 \n" \ + " swi #0 \n" \ + "_dl_printf: \n" \ + " mov pc, lr \n" \ ".previous"); Index: gnu/gcc/gcc/config/arm/openbsd.h =================================================================== RCS file: /cvs/src/gnu/gcc/gcc/config/arm/openbsd.h,v retrieving revision 1.6 diff -u -p -r1.6 openbsd.h --- gnu/gcc/gcc/config/arm/openbsd.h 1 Sep 2016 11:03:09 -0000 1.6 +++ gnu/gcc/gcc/config/arm/openbsd.h 7 Sep 2016 20:22:20 -0000 @@ -203,34 +203,12 @@ do \ } \ while (0) -/* Provide a STARTFILE_SPEC appropriate for OpenBSD ELF. Here we - provide support for the special GCC option -static. On ELF - targets, we also add the crtbegin.o file, which provides part - of the support for getting C++ file-scope static objects - constructed before entering "main". */ - -#define OPENBSD_STARTFILE_SPEC \ - "%{!shared: \ - %{pg:gcrt0%O%s} \ - %{!pg: \ - %{p:gcrt0%O%s} \ - %{!p:crt0%O%s}}} \ - %:if-exists(crti%O%s) \ - %{static:%:if-exists-else(crtbeginT%O%s crtbegin%O%s)} \ - %{!static: \ - %{!shared:crtbegin%O%s} %{shared:crtbeginS%O%s}}" - +/* As an elf system, we need crtbegin/crtend stuff. */ #undef STARTFILE_SPEC -#define STARTFILE_SPEC OPENBSD_STARTFILE_SPEC - -/* Provide an ENDFILE_SPEC appropriate for OpenBSD ELF. Here we -add crtend.o, which provides part of the support for getting -C++ file-scope static objects deconstructed after exiting "main". */ - -#define OPENBSD_ENDFILE_SPEC \ - "%{!shared:crtend%O%s} %{shared:crtendS%O%s} \ - %:if-exists(crtn%O%s)" - +#define STARTFILE_SPEC "\ + %{!shared: %{pg:gcrt0%O%s} %{!pg:%{p:gcrt0%O%s} \ + %{!p:%{!static:crt0%O%s} %{static:%{nopie:crt0%O%s} \ + %{!nopie:rcrt0%O%s}}}} \ + crtbegin%O%s} %{shared:crtbeginS%O%s}" #undef ENDFILE_SPEC -#define ENDFILE_SPEC OPENBSD_ENDFILE_SPEC - +#define ENDFILE_SPEC "%{!shared:crtend%O%s} %{shared:crtendS%O%s}" Index: share/mk/bsd.own.mk =================================================================== RCS file: /cvs/src/share/mk/bsd.own.mk,v retrieving revision 1.177 diff -u -p -r1.177 bsd.own.mk --- share/mk/bsd.own.mk 3 Sep 2016 10:47:24 -0000 1.177 +++ share/mk/bsd.own.mk 7 Sep 2016 20:22:20 -0000 @@ -19,7 +19,7 @@ GCC3_ARCH=m88k # m88k: ? PIE_ARCH=alpha amd64 arm hppa i386 mips64 mips64el powerpc sh sparc64 -STATICPIE_ARCH=alpha amd64 hppa i386 mips64 mips64el powerpc sh sparc64 +STATICPIE_ARCH=alpha amd64 arm hppa i386 mips64 mips64el powerpc sh sparc64 .for _arch in ${MACHINE_ARCH} .if !empty(GCC3_ARCH:M${_arch})