Blob Blame History Raw
From 0321eecc6d29d5b571930ed97f392623fe80a2e5 Mon Sep 17 00:00:00 2001
From: Ayke van Laethem <aykevanlaethem@gmail.com>
Date: Sat, 23 Sep 2023 14:57:25 +0200
Subject: [PATCH 6/7] avr: don't compile large parts of picolibc (math, stdio)

These parts aren't critical and lead to crashes on small chips without
long jumps (like the attiny85) with LLVM 17. (Older LLVM versions would
emit long jumps regardless, even if the chip didn't support those).

For more information, see: https://github.com/llvm/llvm-project/issues/67042

Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
---
 builder/picolibc.go | 172 ++++++++++++++++++++++++--------------------
 1 file changed, 93 insertions(+), 79 deletions(-)

diff --git a/builder/picolibc.go b/builder/picolibc.go
index 1b7c748b..91ad27be 100644
--- a/builder/picolibc.go
+++ b/builder/picolibc.go
@@ -3,6 +3,7 @@ package builder
 import (
 	"os"
 	"path/filepath"
+	"strings"
 
 	"github.com/tinygo-org/tinygo/goenv"
 )
@@ -41,91 +42,23 @@ var Picolibc = Library{
 	},
 	sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib") },
 	librarySources: func(target string) ([]string, error) {
-		return picolibcSources, nil
+		sources := append([]string(nil), picolibcSources...)
+		if !strings.HasPrefix(target, "avr") {
+			// Small chips without long jumps can't compile many files (printf,
+			// pow, etc). Therefore exclude those source files for those chips.
+			// Unfortunately it's difficult to exclude only some chips, so this
+			// excludes those files on all AVR chips for now.
+			// More information:
+			// https://github.com/llvm/llvm-project/issues/67042
+			sources = append(sources, picolibcSourcesLarge...)
+		}
+		return sources, nil
 	},
 }
 
 var picolibcSources = []string{
 	"../../picolibc-stdio.c",
 
-	// srcs_tinystdio
-	"libc/tinystdio/asprintf.c",
-	"libc/tinystdio/bufio.c",
-	"libc/tinystdio/clearerr.c",
-	"libc/tinystdio/ecvt_r.c",
-	"libc/tinystdio/ecvt.c",
-	"libc/tinystdio/ecvtf_r.c",
-	"libc/tinystdio/ecvtf.c",
-	"libc/tinystdio/fcvt.c",
-	"libc/tinystdio/fcvt_r.c",
-	"libc/tinystdio/fcvtf.c",
-	"libc/tinystdio/fcvtf_r.c",
-	"libc/tinystdio/gcvt.c",
-	"libc/tinystdio/gcvtf.c",
-	"libc/tinystdio/fclose.c",
-	"libc/tinystdio/fdevopen.c",
-	"libc/tinystdio/feof.c",
-	"libc/tinystdio/ferror.c",
-	"libc/tinystdio/fflush.c",
-	"libc/tinystdio/fgetc.c",
-	"libc/tinystdio/fgets.c",
-	"libc/tinystdio/fileno.c",
-	"libc/tinystdio/filestrget.c",
-	"libc/tinystdio/filestrput.c",
-	"libc/tinystdio/filestrputalloc.c",
-	"libc/tinystdio/fmemopen.c",
-	"libc/tinystdio/fprintf.c",
-	"libc/tinystdio/fputc.c",
-	"libc/tinystdio/fputs.c",
-	"libc/tinystdio/fread.c",
-	//"libc/tinystdio/freopen.c", // crashes with AVR, see: https://github.com/picolibc/picolibc/pull/369
-	"libc/tinystdio/fscanf.c",
-	"libc/tinystdio/fseek.c",
-	"libc/tinystdio/fseeko.c",
-	"libc/tinystdio/ftell.c",
-	"libc/tinystdio/ftello.c",
-	"libc/tinystdio/fwrite.c",
-	"libc/tinystdio/getchar.c",
-	"libc/tinystdio/gets.c",
-	"libc/tinystdio/matchcaseprefix.c",
-	"libc/tinystdio/mktemp.c",
-	"libc/tinystdio/perror.c",
-	"libc/tinystdio/printf.c",
-	"libc/tinystdio/putchar.c",
-	"libc/tinystdio/puts.c",
-	"libc/tinystdio/rewind.c",
-	"libc/tinystdio/scanf.c",
-	"libc/tinystdio/setbuf.c",
-	"libc/tinystdio/setbuffer.c",
-	"libc/tinystdio/setlinebuf.c",
-	"libc/tinystdio/setvbuf.c",
-	"libc/tinystdio/snprintf.c",
-	"libc/tinystdio/sprintf.c",
-	"libc/tinystdio/snprintfd.c",
-	"libc/tinystdio/snprintff.c",
-	"libc/tinystdio/sprintff.c",
-	"libc/tinystdio/sprintfd.c",
-	"libc/tinystdio/sscanf.c",
-	"libc/tinystdio/strfromf.c",
-	"libc/tinystdio/strfromd.c",
-	"libc/tinystdio/strtof.c",
-	"libc/tinystdio/strtof_l.c",
-	"libc/tinystdio/strtod.c",
-	"libc/tinystdio/strtod_l.c",
-	"libc/tinystdio/ungetc.c",
-	"libc/tinystdio/vasprintf.c",
-	"libc/tinystdio/vfiprintf.c",
-	"libc/tinystdio/vfprintf.c",
-	"libc/tinystdio/vfprintff.c",
-	"libc/tinystdio/vfscanf.c",
-	"libc/tinystdio/vfiscanf.c",
-	"libc/tinystdio/vfscanff.c",
-	"libc/tinystdio/vprintf.c",
-	"libc/tinystdio/vscanf.c",
-	"libc/tinystdio/vsscanf.c",
-	"libc/tinystdio/vsnprintf.c",
-	"libc/tinystdio/vsprintf.c",
-
 	"libc/string/bcmp.c",
 	"libc/string/bcopy.c",
 	"libc/string/bzero.c",
@@ -229,6 +162,87 @@ var picolibcSources = []string{
 	"libc/string/wmempcpy.c",
 	"libc/string/wmemset.c",
 	"libc/string/xpg_strerror_r.c",
+}
+
+// Parts of picolibc that are too large for small AVRs.
+var picolibcSourcesLarge = []string{
+	// srcs_tinystdio
+	"libc/tinystdio/asprintf.c",
+	"libc/tinystdio/bufio.c",
+	"libc/tinystdio/clearerr.c",
+	"libc/tinystdio/ecvt_r.c",
+	"libc/tinystdio/ecvt.c",
+	"libc/tinystdio/ecvtf_r.c",
+	"libc/tinystdio/ecvtf.c",
+	"libc/tinystdio/fcvt.c",
+	"libc/tinystdio/fcvt_r.c",
+	"libc/tinystdio/fcvtf.c",
+	"libc/tinystdio/fcvtf_r.c",
+	"libc/tinystdio/gcvt.c",
+	"libc/tinystdio/gcvtf.c",
+	"libc/tinystdio/fclose.c",
+	"libc/tinystdio/fdevopen.c",
+	"libc/tinystdio/feof.c",
+	"libc/tinystdio/ferror.c",
+	"libc/tinystdio/fflush.c",
+	"libc/tinystdio/fgetc.c",
+	"libc/tinystdio/fgets.c",
+	"libc/tinystdio/fileno.c",
+	"libc/tinystdio/filestrget.c",
+	"libc/tinystdio/filestrput.c",
+	"libc/tinystdio/filestrputalloc.c",
+	"libc/tinystdio/fmemopen.c",
+	"libc/tinystdio/fprintf.c",
+	"libc/tinystdio/fputc.c",
+	"libc/tinystdio/fputs.c",
+	"libc/tinystdio/fread.c",
+	//"libc/tinystdio/freopen.c", // crashes with AVR, see: https://github.com/picolibc/picolibc/pull/369
+	"libc/tinystdio/fscanf.c",
+	"libc/tinystdio/fseek.c",
+	"libc/tinystdio/fseeko.c",
+	"libc/tinystdio/ftell.c",
+	"libc/tinystdio/ftello.c",
+	"libc/tinystdio/fwrite.c",
+	"libc/tinystdio/getchar.c",
+	"libc/tinystdio/gets.c",
+	"libc/tinystdio/matchcaseprefix.c",
+	"libc/tinystdio/mktemp.c",
+	"libc/tinystdio/perror.c",
+	"libc/tinystdio/printf.c",
+	"libc/tinystdio/putchar.c",
+	"libc/tinystdio/puts.c",
+	"libc/tinystdio/rewind.c",
+	"libc/tinystdio/scanf.c",
+	"libc/tinystdio/setbuf.c",
+	"libc/tinystdio/setbuffer.c",
+	"libc/tinystdio/setlinebuf.c",
+	"libc/tinystdio/setvbuf.c",
+	"libc/tinystdio/snprintf.c",
+	"libc/tinystdio/sprintf.c",
+	"libc/tinystdio/snprintfd.c",
+	"libc/tinystdio/snprintff.c",
+	"libc/tinystdio/sprintff.c",
+	"libc/tinystdio/sprintfd.c",
+	"libc/tinystdio/sscanf.c",
+	"libc/tinystdio/strfromf.c",
+	"libc/tinystdio/strfromd.c",
+	"libc/tinystdio/strtof.c",
+	"libc/tinystdio/strtof_l.c",
+	"libc/tinystdio/strtod.c",
+	"libc/tinystdio/strtod_l.c",
+	"libc/tinystdio/ungetc.c",
+	"libc/tinystdio/vasprintf.c",
+	"libc/tinystdio/vfiprintf.c",
+	"libc/tinystdio/vfprintf.c",
+	"libc/tinystdio/vfprintff.c",
+	"libc/tinystdio/vfscanf.c",
+	"libc/tinystdio/vfiscanf.c",
+	"libc/tinystdio/vfscanff.c",
+	"libc/tinystdio/vprintf.c",
+	"libc/tinystdio/vscanf.c",
+	"libc/tinystdio/vsscanf.c",
+	"libc/tinystdio/vsnprintf.c",
+	"libc/tinystdio/vsprintf.c",
 
 	"libm/common/sf_finite.c",
 	"libm/common/sf_copysign.c",
-- 
2.41.0