mailing list of musl libc
 help / color / mirror / code / Atom feed
* sh2 support is now upstream in musl
@ 2015-06-16 16:30 Rich Felker
  0 siblings, 0 replies; only message in thread
From: Rich Felker @ 2015-06-16 16:30 UTC (permalink / raw)
  To: musl; +Cc: D. Jeff Dionne, Yoshinori Sato, shumpei.kawasaki, Rob Landley

[-- Attachment #1: Type: text/plain, Size: 3296 bytes --]

As of commit 10d0268ccfab9152250eeeed3952ce3fed44131a, musl now
supports sh2. Some kernel and toolchain tweaks are needed to make this
support useful, though:

- The kernel must be patched to support ELF binaries on NOMMU and to
  accept the new unified sh syscal trap number 31. See attached
  patches.

- My toolchain was built with musl-cross patches to gcc 4.7.3 (newer
  gcc versions break the kernel due to referencing libgcc symbols the
  kernel doesn't provide) using TRIPLE=sheb-linux-musl. I was not able
  to get it to recognize sh2eb or similar as big-endian, and I also
  ended up adding --with-endian=big to GCC_CONFFLAGS just to be safe
  (see musl-cross's config.sh).

- All versions of GCC I tried had register allocation errors compiling
  musl's complex math code as PIC. I've attached a patch that does a
  cheap CFLAGS hack in the makefile to work around these compiler
  bugs.

- All executables must be built as PIE. If you only use dynamic
  linking, just passing -fPIE and -pie in CFLAGS and LDFLAGS should be
  sufficient as long as you remember to do so.
  
- For static linked PIE, changes to gcc's specs are needed, and it may
  be desirable to make changes even if you're doing dynamic linking so
  you don't get breakage from forgeting the flags. It's also necessary
  to add -fPIC to musl's CFLAGS so that libc.a is built in a form
  that's compatible with linking into a PIE program.

Details on specs changes (these are not given as a patch because the
patch will be highly GCC version-/build-specific):

At the beginning of cc1_options, add:

	%{D__KERNEL__:;:-fPIE}

Yes the conditional is hideously ugly and "wrong" but it's what the
"esp" patches from Gentoo do and it's needed to avoid horribly
breaking the kernel.

For endfile, change:

	%{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s

or similar to:

	crtendS.o%s crtn.o%s

i.e. always use crtendS.o (the PIC/PIE compatible version) instead of
the default PIC-incompatible crtend.o. (GCC really should just remove
the PIC-incompatible versions alltogether; they're not useful.) The
same should be done for crtbegin in startfile, but it also needs crt1
work. Change it from something like:

	%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}}    crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}

to:

	%{!shared: %{static:rcrt1.o%s;:Scrt1.o%s}}    crti.o%s crtbeginS.o%s

Finally, in link_command, %{pie:-pie} should be replaced with:

	%{pie:} %{!nostdlib:%{!r:%{static:-shared -Bsymbolic;:-pie}}}

This causes -pie to always be passed to the linker when producing a
dynamic-linked executable, and enables static-PIE via some magic
linker options when -static is used. This could be handled much better
at the binutils level.

One more thing -- I also changed libgcc to simply be:

	-lgcc -lgcc_eh

so that libgcc_s.so is never used. This change should not be necessary
but is preferable anyway.

The above hacking on the specfile can be performed by invoking gcc
with -dumpspecs, saving the output in gcc's lib dir (the one
containing libgcc.a), and editing it. A better approach, however,
would be to edit the GCC source files that the internal specs are
generated from. The widely-used ESP patches (for PIE & SSP by default)
would be a starting point for this.

Rich

[-- Attachment #2: fdpic_elf_loader.diff --]
[-- Type: text/plain, Size: 1969 bytes --]

--- fs/binfmt_elf_fdpic.c.orig
+++ fs/binfmt_elf_fdpic.c
@@ -103,14 +103,27 @@
 core_initcall(init_elf_fdpic_binfmt);
 module_exit(exit_elf_fdpic_binfmt);
 
+static int is_fdpic(struct elfhdr *hdr)
+{
+#ifdef CONFIG_MMU
+	return 1;
+#else
+	return elf_check_fdpic(hdr);
+#endif
+}
+
 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
 {
 	if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
 		return 0;
 	if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
 		return 0;
-	if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
+	if (!elf_check_arch(hdr))
 		return 0;
+#ifdef CONFIG_MMU
+	if (!elf_check_fdpic(hdr))
+		return 0;
+#endif
 	if (!file->f_op->mmap)
 		return 0;
 	return 1;
@@ -269,7 +282,7 @@
 
 	}
 
-	if (elf_check_const_displacement(&exec_params.hdr))
+	if (elf_check_const_displacement(&exec_params.hdr) || !is_fdpic(&exec_params.hdr))
 		exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
 	/* perform insanity checks on the interpreter */
@@ -306,9 +319,9 @@
 
 	retval = -ENOEXEC;
 	if (stack_size == 0)
-		goto error;
+		stack_size = 131072;
 
-	if (elf_check_const_displacement(&interp_params.hdr))
+	if (elf_check_const_displacement(&interp_params.hdr) || !is_fdpic(&interp_params.hdr))
 		interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
 	/* flush all traces of the currently running executable */
@@ -319,7 +332,8 @@
 	/* there's now no turning back... the old userspace image is dead,
 	 * defunct, deceased, etc.
 	 */
-	set_personality(PER_LINUX_FDPIC);
+	if (is_fdpic(&exec_params.hdr))
+		set_personality(PER_LINUX_FDPIC);
 	if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
 		current->personality |= READ_IMPLIES_EXEC;
 
@@ -400,8 +414,6 @@
 
 	current->mm->brk = current->mm->start_brk;
 	current->mm->context.end_brk = current->mm->start_brk;
-	current->mm->context.end_brk +=
-		(stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
 	current->mm->start_stack = current->mm->start_brk + stack_size;
 #endif
 

[-- Attachment #3: sh-unified-syscall.diff --]
[-- Type: text/plain, Size: 654 bytes --]

--- arch/sh/kernel/cpu/sh2/entry.S.orig
+++ arch/sh/kernel/cpu/sh2/entry.S
@@ -144,9 +144,9 @@
 	mov	#64,r8
 	cmp/hs	r8,r9
 	bt	interrupt_entry	! vec >= 64 is interrupt
-	mov	#32,r8
+	mov	#31,r8
 	cmp/hs	r8,r9
-	bt	trap_entry	! 64 > vec >= 32  is trap
+	bt	trap_entry	! 64 > vec >= 31  is trap
 
 	mov.l	4f,r8
 	mov	r9,r4
@@ -178,9 +178,9 @@
 
 trap_entry:
 	mov	#0x30,r8
-	cmp/ge	r8,r9		! vector 0x20-0x2f is systemcall
+	cmp/ge	r8,r9		! vector 0x1f-0x2f is systemcall
 	bt	1f
-	add	#-0x10,r9	! convert SH2 to SH3/4 ABI
+	mov     #0x1f,r9	! convert to unified SH2/3/4 trap number
 1:	
 	shll2	r9			! TRA
 	bra	system_call	! jump common systemcall entry

[-- Attachment #4: musl_sh2_complex_cflags.diff --]
[-- Type: text/plain, Size: 607 bytes --]

diff --git a/Makefile b/Makefile
index 2eb7b30..c4dec95 100644
--- a/Makefile
+++ b/Makefile
@@ -108,6 +108,9 @@ $(NOSSP_SRCS:%.c=%.o) $(NOSSP_SRCS:%.c=%.lo): CFLAGS += $(CFLAGS_NOSSP)
 
 $(CRT_LIBS:lib/%=crt/%): CFLAGS += -DCRT
 
+$(patsubst %.c,%.o,$(wildcard src/complex/*.c)): CFLAGS += -fno-optimize-sibling-calls
+$(patsubst %.c,%.lo,$(wildcard src/complex/*.c)): CFLAGS += -fno-optimize-sibling-calls
+
 # This incantation ensures that changes to any subarch asm files will
 # force the corresponding object file to be rebuilt, even if the implicit
 # rule below goes indirectly through a .sub file.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-06-16 16:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-16 16:30 sh2 support is now upstream in musl Rich Felker

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