newlib: Use mcmodel=medany for RISCV64
PATCH 1/2 newlib: Use mcmodel=medany for RISCV64
It was previously discovered that mcmodel=medany should be used for RISCV64, however this was only being set for the applications themselves, but not for newlib, this meant that we ended up with C library that used a code model and an application that used another one which is not something we want.
Pass mcmodel=medany when building newlib for RISCV64 as well.
Also, s/CFLAGS/TARGET_CFLAGS to standarize across recipes, the variable expansion provides no functional difference at this point.
--- meta/classes-recipe/baremetal-image.bbclass | 2 +- meta/recipes-core/newlib/newlib.inc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)
meta/classes-recipe/baremetal-image.bbclass +1 -1
@@ -103,7 +103,7 @@ QB_OPT_APPEND:append:qemuriscv32 = " -bios none" # since medlow can only access addresses below 0x80000000 and RAM # starts at 0x80000000 on RISC-V 64 # Keep RISC-V 32 using -mcmodel=medlow (symbols lie between -2GB:2GB) -CFLAGS:append:qemuriscv64 = " -mcmodel=medany" +TARGET_CFLAGS:append:qemuriscv64 = " -mcmodel=medany" ## Emulate image.bbclass
meta/recipes-core/newlib/newlib.inc +8 -0
@@ -28,6 +28,14 @@ B = "${WORKDIR}/build" ## disable stdlib TARGET_CC_ARCH:append = " -nostdlib" +# Both the C library and the application should share the same mcmodel. +# Use the medium-any code model for the RISC-V 64 bit implementation, +# since medlow can only access addresses below 0x80000000 and RAM +# starts at 0x80000000 on RISC-V 64 +# Keep RISC-V 32 using -mcmodel=medlow (symbols lie between -2GB:2GB) +TARGET_CFLAGS:append:qemuriscv64 = " -mcmodel=medany" + + EXTRA_OECONF = " \ --build=${BUILD_SYS} \ --target=${TARGET_SYS} \
Signature · 1 line
2.45.1
PATCH v3 2/2 tclibc-picolibc: Adds a new TCLIBC variant to build with picolibc as C library
Enables usage of TCLIBC=picolibc extending OE functionality to build and use picolibc based toolchains to build baremetal applications.
Picolibc is a set of standard C libraries, both libc and libm, designed for smaller embedded systems with limited ROM and RAM. Picolibc includes code from Newlib and AVR Libc, but adresses some of newlibs concerns, it retains newlibs directory structure, math, string and locale implementations, but removed the GPL bits used to build the library, swiches old C style code for C18 and replaces autotools with meson.
This patch adds a picolibc recipe for the C library, a picolibc-helloworld recipe that contains an example application and a testcase that builds it.
Picolibc can be built for ARM and RISCV architectures, its been tested both for 32 and 64 bits, the provided example recipe produces the following output:
hello, world
Runqemu does not automatically show any output since it hides QEMU stderr which is where the QEMU monitors output is directed to when using semihosting, but, manually running the same QEMU command does work properly.
--- meta/classes-recipe/baremetal-image.bbclass | 4 +- meta/classes-recipe/cross-canadian.bbclass | 2 +- meta/conf/distro/include/maintainers.inc | 2 + meta/conf/distro/include/tclibc-picolibc.inc | 40 ++++++ meta/conf/documentation.conf | 2 +- .../conf/machine/include/riscv/arch-riscv.inc | 1 + meta/lib/oeqa/selftest/cases/distrodata.py | 2 +- meta/lib/oeqa/selftest/cases/picolibc.py | 18 +++ .../picolibc/picolibc-helloworld_git.bb | 40 ++++++ meta/recipes-core/picolibc/picolibc.inc | 21 ++++ .../avoid_polluting_cross_directories.patch | 119 ++++++++++++++++++ .../picolibc/no-early-compiler-checks.cross | 6 + meta/recipes-core/picolibc/picolibc_git.bb | 35 ++++++ meta/recipes-devtools/gcc/gcc-cross.inc | 1 + meta/recipes-devtools/gcc/gcc-runtime.inc | 3 + meta/recipes-devtools/gcc/libgcc-common.inc | 5 + 16 files changed, 296 insertions(+), 5 deletions(-) create mode 100644 meta/conf/distro/include/tclibc-picolibc.inc create mode 100644 meta/lib/oeqa/selftest/cases/picolibc.py create mode 100644 meta/recipes-core/picolibc/picolibc-helloworld_git.bb create mode 100644 meta/recipes-core/picolibc/picolibc.inc create mode 100644 meta/recipes-core/picolibc/picolibc/avoid_polluting_cross_directories.patch create mode 100644 meta/recipes-core/picolibc/picolibc/no-early-compiler-checks.cross create mode 100644 meta/recipes-core/picolibc/picolibc_git.bb
meta/classes-recipe/baremetal-image.bbclass +2 -2
@@ -16,8 +16,8 @@ # See meta-skeleton for a working example. -# Toolchain should be baremetal or newlib based. -# TCLIBC="baremetal" or TCLIBC="newlib" +# Toolchain should be baremetal or newlib/picolibc based. +# TCLIBC="baremetal" or TCLIBC="newlib" or TCLIBC="picolibc" COMPATIBLE_HOST:libc-musl:class-target = "null" COMPATIBLE_HOST:libc-glibc:class-target = "null"
meta/classes-recipe/cross-canadian.bbclass +1 -1
@@ -36,7 +36,7 @@ python () { if d.getVar("MODIFYTOS") != "1": return - if d.getVar("TCLIBC") in [ 'baremetal', 'newlib' ]: + if d.getVar("TCLIBC") in [ 'baremetal', 'newlib', 'picolibc' ]: return tos = d.getVar("TARGET_OS")
meta/conf/distro/include/maintainers.inc +2 -0
@@ -579,6 +579,8 @@ RECIPE_MAINTAINER:pn-pcmanfm = "Alexander Kanavin <[email protected]>" RECIPE_MAINTAINER:pn-perf = "Bruce Ashfield <[email protected]>" RECIPE_MAINTAINER:pn-perl = "Alexander Kanavin <[email protected]>" RECIPE_MAINTAINER:pn-perlcross = "Alexander Kanavin <[email protected]>" +RECIPE_MAINTAINER:pn-picolibc = "Alejandro Hernandez <[email protected]>" +RECIPE_MAINTAINER:pn-picolibc-helloworld = "Alejandro Hernandez <[email protected]>" RECIPE_MAINTAINER:pn-piglit = "Ross Burton <[email protected]>" RECIPE_MAINTAINER:pn-pigz = "Hongxu Jia <[email protected]>" RECIPE_MAINTAINER:pn-pinentry = "Unassigned <[email protected]>"
meta/conf/distro/include/tclibc-picolibc.inc new file +40 -0
@@ -0,0 +1,40 @@ +# +# Picolibc configuration +# + +LIBCEXTENSION = "-picolibc" +LIBCOVERRIDE = ":libc-picolibc" + +PREFERRED_PROVIDER_virtual/libc ?= "picolibc" +PREFERRED_PROVIDER_virtual/libiconv ?= "picolibc" +PREFERRED_PROVIDER_virtual/libintl ?= "picolibc" +PREFERRED_PROVIDER_virtual/nativesdk-libintl ?= "nativesdk-glibc" +PREFERRED_PROVIDER_virtual/nativesdk-libiconv ?= "nativesdk-glibc" + +DISTRO_FEATURES_BACKFILL_CONSIDERED += "ldconfig" + +IMAGE_LINGUAS = "" + +LIBC_DEPENDENCIES = " \ + picolibc-dbg \ + picolibc-dev \ + libgcc-dev \ + libgcc-dbg \ + libstdc++-dev \ + libstdc++-staticdev \ +" + +ASSUME_PROVIDED += "virtual/crypt" + +TARGET_OS = "elf" +TARGET_OS:arm = "eabi" + +TOOLCHAIN_HOST_TASK ?= "packagegroup-cross-canadian-${MACHINE} nativesdk-qemu nativesdk-sdk-provides-dummy" +TOOLCHAIN_TARGET_TASK ?= "${LIBC_DEPENDENCIES}" +TOOLCHAIN_NEED_CONFIGSITE_CACHE:remove = "zlib ncurses" + +# RISCV linker doesnt support PIE +SECURITY_CFLAGS:libc-picolibc:qemuriscv32 = "${SECURITY_NOPIE_CFLAGS}" +SECURITY_CFLAGS:libc-picolibc:qemuriscv64 = "${SECURITY_NOPIE_CFLAGS}" + +
meta/conf/documentation.conf +1 -1
@@ -421,7 +421,7 @@ TARGET_FPU[doc] = "Specifies the method for handling FPU code. For FPU-less targ TARGET_OS[doc] = "Specifies the target's operating system." TARGET_PREFIX[doc] = "The prefix for the cross-compile toolchain (e.g. arm-linux-)." TARGET_SYS[doc] = "The target system is comprised of TARGET_ARCH,TARGET_VENDOR and TARGET_OS." -TCLIBC[doc] = "Specifies C library (libc) variant to use during the build process. You can select 'baremetal', 'glibc', 'musl' or 'newlib'." +TCLIBC[doc] = "Specifies C library (libc) variant to use during the build process. You can select 'baremetal', 'glibc', 'musl', 'newlib', or 'picolibc'." TCMODE[doc] = "Enables an external toolchain (where provided by an additional layer) if set to a value other than 'default'." TESTIMAGE_AUTO[doc] = "Enables test booting of virtual machine images under the QEMU emulator after any root filesystems are created and runs tests against those images each time an image is built." TEST_QEMUBOOT_TIMEOUT[doc] = "The time in seconds allowed for an image to boot before automated runtime tests begin to run against an image."
meta/conf/machine/include/riscv/arch-riscv.inc +1 -0
@@ -11,5 +11,6 @@ TUNE_CCARGS:append = "${@bb.utils.contains('TUNE_FEATURES', 'riscv64nc', ' -marc # Fix: ld: unrecognized option '--hash-style=sysv' LINKER_HASH_STYLE:libc-newlib = "" +LINKER_HASH_STYLE:libc-picolibc = "" # Fix: ld: unrecognized option '--hash-style=gnu' LINKER_HASH_STYLE:libc-baremetal = ""
meta/lib/oeqa/selftest/cases/distrodata.py +1 -1
@@ -55,7 +55,7 @@ but their recipes claim otherwise by setting UPSTREAM_VERSION_UNKNOWN. Please re return False def is_maintainer_exception(entry): - exceptions = ["musl", "newlib", "linux-yocto", "linux-dummy", "mesa-gl", "libgfortran", "libx11-compose-data", + exceptions = ["musl", "newlib", "picolibc", "linux-yocto", "linux-dummy", "mesa-gl", "libgfortran", "libx11-compose-data", "cve-update-nvd2-native",] for i in exceptions: if i in entry:
meta/lib/oeqa/selftest/cases/picolibc.py new file +18 -0
@@ -0,0 +1,18 @@ +# +# Copyright OpenEmbedded Contributors +# +# SPDX-License-Identifier: MIT +# + +from oeqa.selftest.case import OESelftestTestCase +from oeqa.utils.commands import bitbake, get_bb_var + +class PicolibcTest(OESelftestTestCase): + + def test_picolibc(self): + compatible_machines = ['qemuarm', 'qemuarm64', 'qemuriscv32', 'qemuriscv64'] + machine = get_bb_var('MACHINE') + if machine not in compatible_machines: + self.skipTest('This test only works with machines : %s' % ' '.join(compatible_machines)) + self.write_config('TCLIBC = "picolibc"') + bitbake("picolibc-helloworld")
meta/recipes-core/picolibc/picolibc-helloworld_git.bb new file +40 -0
@@ -0,0 +1,40 @@ +require picolibc.inc + +# baremetal-image overrides +BAREMETAL_BINNAME ?= "hello_picolibc_${MACHINE}" +IMAGE_LINK_NAME ?= "baremetal-picolibc-image-${MACHINE}" +IMAGE_NAME_SUFFIX ?= "" +QB_DEFAULT_KERNEL ?= "${IMAGE_LINK_NAME}.elf" + +inherit baremetal-image + +COMPATIBLE_MACHINE = "qemuarm|qemuarm64|qemuriscv32|qemuriscv64" + +# Use semihosting to test via QEMU +QB_OPT_APPEND:append = " -semihosting-config enable=on" + +# picolibc comes with a set of linker scripts, set the file +# according to the architecture being built. +PICOLIBC_LINKERSCRIPT:qemuarm64 = "aarch64.ld" +PICOLIBC_LINKERSCRIPT:qemuarm = "arm.ld" +PICOLIBC_LINKERSCRIPT:qemuriscv32 = "riscv.ld" +PICOLIBC_LINKERSCRIPT:qemuriscv64 = "riscv.ld" + +# Simple compile function that manually exemplifies usage; as noted, +# use a custom linker script, the GCC specs provided by picolibc +# and semihost to be able to test via QEMU's monitor +do_compile(){ + ${CC} ${CFLAGS} ${LDFLAGS} --verbose -T${S}/hello-world/${PICOLIBC_LINKERSCRIPT} -specs=picolibc.specs --oslib=semihost -o ${BAREMETAL_BINNAME}.elf ${S}/hello-world/hello-world.c + ${OBJCOPY} -O binary ${BAREMETAL_BINNAME}.elf ${BAREMETAL_BINNAME}.bin +} + +do_install(){ + install -d ${D}/${base_libdir}/firmware + install -m 755 ${B}/${BAREMETAL_BINNAME}.elf ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.elf + install -m 755 ${B}/${BAREMETAL_BINNAME}.bin ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.bin +} + +FILES:${PN} += " \ + ${base_libdir}/firmware/${BAREMETAL_BINNAME}.elf \ + ${base_libdir}/firmware/${BAREMETAL_BINNAME}.bin \ +"
meta/recipes-core/picolibc/picolibc.inc new file +21 -0
@@ -0,0 +1,21 @@ +SUMMARY = "C Libraries for Smaller Embedded Systems" +HOMEPAGE = "https://keithp.com/picolibc" +DESCRIPTION = "Picolibc is a set of standard C libraries, both libc and libm, designed for smaller embedded systems with limited ROM and RAM. Picolibc includes code from Newlib and AVR Libc." +SECTION = "libs" + +# Newlib based code but GPL related bits removed, test/printf-tests.c and test/testcases.c +# are GPLv2 and GeneratePicolibcCrossFile.sh is AGPL3 but not part of the artifacts. +LICENSE = "BSD-2-Clause & BSD-3-Clause" +LIC_FILES_CHKSUM = " \ + file://COPYING.GPL2;md5=59530bdf33659b29e73d4adb9f9f6552 \ + file://COPYING.NEWLIB;md5=08ae03456feb75b81cfdb359e0f1ef85 \ + file://COPYING.picolibc;md5=e50fa9458a40929689861ed472d46bc7 \ + " + +BASEVER = "1.8.6" +PV = "${BASEVER}+git" +SRC_URI = "git://github.com/picolibc/picolibc.git;protocol=https;branch=main" +SRCREV="764ef4e401a8f4c6a86ab723533841f072885a5b" + +S = "${WORKDIR}/git" +B = "${WORKDIR}/build"
meta/recipes-core/picolibc/picolibc/avoid_polluting_cross_directories.patch new file +119 -0
119 lines not on this page · raw message
meta/recipes-core/picolibc/picolibc/no-early-compiler-checks.cross new file +6 -0
@@ -0,0 +1,6 @@ +# We need to explicitly bypass mesons sanity check to avoid early compiler errors +# otherwise meson will try to compile AND run test applications: +# ../git/meson.build:35:0: ERROR: Executables created by c compiler are not runnable... + +[properties] +skip_sanity_check=true
meta/recipes-core/picolibc/picolibc_git.bb new file +35 -0
@@ -0,0 +1,35 @@ +require picolibc.inc + +INHIBIT_DEFAULT_DEPS = "1" +DEPENDS = "virtual/${TARGET_PREFIX}gcc" + +PROVIDES += "virtual/libc virtual/libiconv virtual/libintl" + +COMPATIBLE_HOST:libc-musl:class-target = "null" +COMPATIBLE_HOST:libc-glibc:class-target = "null" +COMPATIBLE_MACHINE = "qemuarm|qemuarm64|qemuriscv32|qemuriscv64" + +SRC_URI:append = " file://avoid_polluting_cross_directories.patch" +SRC_URI:append = " file://no-early-compiler-checks.cross" + +# This is being added by picolibc meson files as well to avoid +# early compiler tests from failing, cant remember why I added it +# to the newlib recipe but I would assume it was for the same reason +TARGET_CC_ARCH:append = " -nostdlib" + +# When using RISCV64 use medany for both C library and application recipes +TARGET_CFLAGS:append:qemuriscv64 = " -mcmodel=medany" + +inherit meson + +MESON_CROSS_FILE:append = " --cross-file=${WORKDIR}/no-early-compiler-checks.cross" + +PACKAGECONFIG ??= " specsdir" +# Install GCC specs on libdir +PACKAGECONFIG[specsdir] = "-Dspecsdir=${libdir},-Dspecsdir=none" + + +FILES:${PN}-dev:append = " ${libdir}/*.specs ${libdir}/*.ld" + +# No rpm package is actually created but -dev depends on it, avoid dnf error +DEV_PKG_DEPENDENCY:libc-picolibc = ""
meta/recipes-devtools/gcc/gcc-cross.inc +1 -0
@@ -34,6 +34,7 @@ EXTRA_OECONF += "\ EXTRA_OECONF:append:libc-baremetal = " --without-headers" EXTRA_OECONF:remove:libc-baremetal = "--enable-threads=posix" EXTRA_OECONF:remove:libc-newlib = "--enable-threads=posix" +EXTRA_OECONF:remove:libc-picolibc = "--enable-threads=posix" EXTRA_OECONF_PATHS = "\ --with-gxx-include-dir=/not/exist${target_includedir}/c++/${BINV} \
meta/recipes-devtools/gcc/gcc-runtime.inc +3 -0
@@ -17,6 +17,7 @@ EXTRA_OECONF_PATHS = "\ EXTRA_OECONF:append:linuxstdbase = " --enable-clocale=gnu" EXTRA_OECONF:append = " --cache-file=${B}/config.cache" EXTRA_OECONF:append:libc-newlib = " --with-newlib --with-target-subdir" +EXTRA_OECONF:append:libc-picolibc = " --with-newlib --with-target-subdir" EXTRA_OECONF:append:libc-baremetal = " --with-target-subdir" # Disable ifuncs for libatomic on arm conflicts -march/-mcpu
@@ -27,6 +28,7 @@ DISABLE_STATIC:class-nativesdk ?= "" # Newlib does not support symbol versioning on libsdtcc++ SYMVERS_CONF:libc-newlib = "" +SYMVERS_CONF:libc-picolibc = "" # Building with thumb enabled on armv6t fails ARM_INSTRUCTION_SET:armv6 = "arm"
@@ -47,6 +49,7 @@ RUNTIMETARGET = "${RUNTIMELIBSSP} libstdc++-v3 libgomp libatomic ${RUNTIMELIBITM " # Only build libstdc++ for newlib RUNTIMETARGET:libc-newlib = "libstdc++-v3" +RUNTIMETARGET:libc-picolibc = "libstdc++-v3" # libiberty # libgfortran needs separate recipe due to libquadmath dependency
meta/recipes-devtools/gcc/libgcc-common.inc +5 -0
@@ -53,6 +53,11 @@ do_install:append:libc-newlib () { rmdir ${D}${base_libdir} fi } +do_install:append:libc-picolibc () { + if [ "${base_libdir}" != "${libdir}" ]; then + rmdir ${D}${base_libdir} + fi +} # No rpm package is actually created but -dev depends on it, avoid dnf error DEV_PKG_DEPENDENCY:libc-baremetal = ""
Signature · 1 line
2.45.1