On Sun, Sep 13, 2026 at 10:08:52PM -0700, Pierrick Bouvier wrote:
> On 9/10/26 11:14 AM, Alex Bennée wrote:
> > From: Daniel P. Berrangé <[email protected]>
> > 
> > When it finds a cross compiler, tests/tcg/meson.build tries
> > building test_cc.c to validate that the compiler works. This
> > includes "stdint.h" as a witness for working C library, however,
> > that is not a good choice as it can be provided by the toolchain
> > alone:
> > 
> >    $ rpm -ql gcc-s390x-linux-gnu  | grep /stdint.h
> >    /usr/lib/gcc/s390x-linux-gnu/16/include/stdint.h
> > 
> > As a result, if you install the s390x GCC cross compiler, but
> > not any C library, meson decides to use the host compiler instead
> > of the container:
> > 
> >      cc for aarch64-softmmu          : aarch64-linux-gnu-gcc (from 
> > 'debian-all-test-cross' container)
> >      cc for loongarch64-softmmu      : loongarch64-unknown-linux-gnu-gcc 
> > (from 'debian-loongarch-cross' container)
> >      cc for riscv64-softmmu          : riscv64-linux-gnu-gcc (from 
> > 'debian-all-test-cross' container)
> >      cc for s390x-softmmu            : s390x-linux-gnu-gcc
> >      cc for x86_64-softmmu           : x86_64-linux-gnu-gcc (from 
> > 'debian-amd64-cross' container)
> > 
> > Eventually this results in failure to run 'make check':
> > 
> > [33/40] Generating tests/tcg/s390x-softmmu-hello with a custom command
> > FAILED: [code=1] tests/tcg/s390x-softmmu-hello.test
> > /usr/bin/s390x-linux-gnu-gcc 
> > /home/berrange/src/virt/qemu/tests/tcg/multiarch/system/hello.c -o 
> > tests/tcg/s390x-softmmu-hello.test -static -MMD -MF 
> > tests/tcg/s390x-softmmu-hello.d -Wall -Werror -O0 -g -fno-strict-aliasing 
> > -nostdlib -ffreestanding -Wa,--noexecstack -I 
> > /home/berrange/src/virt/qemu/tests/tcg/s390x/system/../../minilib 
> > /home/berrange/src/virt/qemu/tests/tcg/s390x/system/../../minilib/printf.c 
> > -I /home/berrange/src/virt/qemu/include/hw/s390x/ipl -march=z13 
> > ../tests/tcg/s390x/system/../head64.S ../tests/tcg/s390x/system/../console.c
> > In file included from ../tests/tcg/s390x/system/../console.c:8:
> > ../tests/tcg/s390x/system/../../../../pc-bios/s390-ccw/sclp.c:11:10: fatal 
> > error: string.h: No such file or directory
> >     11 | #include <string.h>
> >        |          ^~~~~~~~~~
> > compilation terminated.
> > 
> > Adding more include files to the test program ensures the
> > meson probe makes a better decision.
> > 
> > Fixes: e5d084d622b1 (tests/tcg/meson.build: check host cross cc is working)
> > Signed-off-by: Daniel P. Berrangé <[email protected]>
> > Message-ID: <[email protected]>
> > Signed-off-by: Alex Bennée <[email protected]>
> > ---
> >   tests/tcg/test_cc.c | 6 +++++-
> >   1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tests/tcg/test_cc.c b/tests/tcg/test_cc.c
> > index d3614a01fe6..fafee055b40 100644
> > --- a/tests/tcg/test_cc.c
> > +++ b/tests/tcg/test_cc.c
> > @@ -1,7 +1,11 @@
> >   /* SPDX-License-Identifier: GPL-2.0-or-later */
> > -/* Include a standard header to make sure cross compiler provides them */
> > +/* Use some standard headers to ensure the cross compiler provides them */
> >   #include <stdint.h>
> > +#include <string.h>
> > +#include <stdbool.h>
> > +#include <stddef.h>
> > +#include <stdio.h>
> >   int main(void)
> >   {
> 
> Ideally, we should do separate tests for system vs user tests.
> System ones are always compiled with -ffreestanding, while user ones should
> provide a C library.
> 
> From this standard:
> 
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2338r3.html
> ```
> A freestanding C implementation is required to provide the entirety of the
> following headers:
> 
> <float.h>
> <iso646.h>
> <limits.h>
> <stdalign.h>
> <stdarg.h>
> <stdbool.h>
> <stddef.h>
> <stdint.h>
> <stdnoreturn.h>
> Most of <string.h> is required (strdup, strndup, strcoll, strxfrm, and
> strerror are excluded). This includes strtok, which requires global data.
> ```
> 
> Maybe for now we can just remove stdio.h from current patch, as it's not
> supposed to be provided in a freestanding environment.

A huge number of files under tests/tcg/ have a #include <stdio.h>
currently.

In the example build failure in the commit message, we have

  tests/tcg/s390x/system/../console.c

which does

  #include "../../../pc-bios/s390-ccw/sclp.c"

and sclp.c has

  #include "s390-ccw.h"

and that header has:

  #include <stdbool.h>
  #include <stddef.h>
  #include <stdint.h>
  #include <stdio.h>

hence the 4 headers I added to the meson check.

So AFAICT, our code is expecting stdio.h to be provided in a
-ffreestanding build. Have we got a more fundamental mistake
wrt our use of -ffreestanding, such that it is relying on
unintended functionality ?

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|


Reply via email to