Bug 14008

Summary: libtool-cross doesn't compile when using newlib
Product: [Build System, Metadata & Runtime] OE-Core Reporter: Jonathan Richardson <jon.richardson>
Component: devtools / tool chainAssignee: Alejandro Enedino Hernandez Samaniego <aehs29>
Status: RESOLVED NOTABUG QA Contact:
Severity: normal    
Priority: Medium CC: akuster808, alejandro, mark.hatle, meta.mr.watcher, meta.watcher, poky.bs.watcher, poky.watcher, randy.macleod, richard.purdie, scott.branden
Version: 3.1.2   
Target Milestone: 4.2   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: Don't know
Attachments:
Description Flags
example recipe
none
mailing list discussion none

Description Jonathan Richardson 2020-08-13 11:34:46 UTC
Created attachment 4713 [details]
example recipe

When using TCLIBC="newlib" the toolchain fails to build for recipes that use autotools. autotools will pull in libtool-cross if required. autoconf fails when it checks if the compiler works because _exit() isn't defined. 

I added flags to link in gloss-linux but then it complained about no providers for the dev and dbg packages in LIBC_DEPENDENCIES. The next problem is binutils-cross-arm doesn't have the dependencies for gloss. These changes end up causing circular dependencies.

Changes made:

diff --git a/meta/classes/base.bbclass b/meta/classes/base.bbclass
index 6776242740..262db927cd 100644
--- a/meta/classes/base.bbclass
+++ b/meta/classes/base.bbclass
@@ -75,6 +75,7 @@ BASE_DEFAULT_DEPS = "virtual/${TARGET_PREFIX}gcc
virtual/${TARGET_PREFIX}compile
 
 BASEDEPENDS = ""
 BASEDEPENDS_class-target = "${@base_dep_prepend(d)}"
+BASEDEPENDS_class-cross = "${@base_dep_prepend(d)}"
 BASEDEPENDS_class-nativesdk = "${@base_dep_prepend(d)}"
 
 DEPENDS_prepend="${BASEDEPENDS} "
diff --git a/meta/conf/distro/include/tclibc-newlib.inc
b/meta/conf/distro/include/tclibc-newlib.inc
index bf89b11288..cb55f3a82c 100644
--- a/meta/conf/distro/include/tclibc-newlib.inc
+++ b/meta/conf/distro/include/tclibc-newlib.inc
@@ -18,13 +18,10 @@ DISTRO_FEATURES_BACKFILL_CONSIDERED += "ldconfig"
 IMAGE_LINGUAS = ""
 
 LIBC_DEPENDENCIES = "\
-    newlib-dbg \
     newlib-dev \
     libgloss \
     libgloss-dev \
-    libgloss-dbg \
     libgcc-dev \
-    libgcc-dbg \
     libstdc++-dev \
     libstdc++-staticdev \
     "
@@ -34,6 +31,7 @@ ASSUME_PROVIDED += "virtual/crypt"
 # Its useful to be able to extend newlib, but we dont provide a
native variant of libgloss
 NEWLIB_EXTENDED ?=  "libgloss libgcc"
 BASE_DEFAULT_DEPS_append_class-target = " ${NEWLIB_EXTENDED}"
+BASE_DEFAULT_DEPS_append_class-cross = " ${NEWLIB_EXTENDED}"
 
 TARGET_OS = "elf"
 TARGET_OS_arm = "eabi"
@@ -45,3 +43,9 @@ TOOLCHAIN_NEED_CONFIGSITE_CACHE_remove = "zlib ncurses"
 # disable pie security flags by default
 SECURITY_CFLAGS_libc-newlib = "${SECURITY_NOPIE_CFLAGS}"
 SECURITY_LDFLAGS_libc-newlib = ""
+
+EXTRA_OECONF_append_class-target = " LIBS=-lgloss-linux"
+EXTRA_OECONF_append_class-cross = " LIBS=-lgloss-linux"
+# binutils-cross.inc sets INHIBIT_DEFAULT_DEPS = "1" which will cause
BASEDEBPENDS to not
+# include libgloss which causes the autoconf compiler check to fail.
+DEPENDS_append_pn-binutils-cross-arm = " libgloss"

To reproduce the issue:
build/conf/local.conf:

MACHINE = "cortexm0"
TCLIBC = "newlib"

conf/machine/cortexm0.conf:
DEFAULTTUNE = "armv6t-novfp"
require conf/machine/include/arm/arch-armv8a.inc

Apply test recipe attached, then run: bitbake helloworld
Comment 1 Jonathan Richardson 2020-08-13 11:35:43 UTC
Created attachment 4714 [details]
mailing list discussion
Comment 2 Alejandro 2023-01-16 23:54:20 UTC
I took a look at this again, as stated the problem itself is that during do_configure, the configure script tries to compile a simple program

$ cat conftest.c

int
main (void)
{

  ;
  return 0;
}


And while linking it throws an error regarding an undefined reference to _exit (this can easily be reproduced manually, no need to issue the configure script):

ld: qemuarm64/tmp/work/cortexa57-poky-elf/libtool-cross/2.4.7-r0/recipe-sysroot/usr/lib/libg.a(lib_a-exit.o): in function `exit':       
/usr/src/debug/newlib/4.2.0-r0/newlib/libc/stdlib/exit.c:64: undefined reference to `_exit'

Newlib/libgloss doesnt provide such syscall but rather the expectation is that developers define one for their own architecture/product.

So, it makes sense that this fails, there's no such implementation.


Now, why does something like baremetal-helloworld successfully compiles/links/runs when TCLIBC="newlib"?

Because for that "product" a custom linkerscript was designed and its overriding the default linker script that the configure script uses for libtool-cross, the custom linkerscript doesnt require the _exit symbol (not implemented because its a simple helloworld), hence no undefined reference error at all.

I have created a simple _exit function to test this, and can confirm that  the compilation of such program succeeds:

// provide _exit (noreturn function)
void _exit(int e){
  for(;;) __asm__ volatile("brk #0\n");
}

# After compiling conftest.c:
$ file conftest
conftest: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped

I do not believe this is a bug, its possible to implement this (at least this part, its unclear whether or not there will be other errors since libtool isnt really designed to work with baremetal targets AFAIC), but we would have to define an _exit() syscall in our libgloss for every architecture that we support, providing a simple stub that just errors when called would allow the linker to succeed, but it would likely be implemented using assembly language which varies from architecture to architecture, this IMO is out of the scope of the project and its something that the product developers should do instead (probably as a bbappend to libgloss in their bsp layer).
Comment 3 Randy MacLeod 2023-01-19 16:10:39 UTC
As per last comment, this isn't really a bug.