How do get uninitialzed variable warning from Clang 2.9? - clang

I have this file:
#include <stdio.h>
int main(void)
{
int i;
printf("%d\n",i);
return 0;
}
If I compile with gcc 4.6.1 on Mint 12:
CC -Wall -O test_gcc.c
test_gcc.c: In function ‘main’:
test_gcc.c:8:10: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
Those same options with clang 2.9 do not give a warning.
The Clang online manual shows a -Weverything, but this version doesn't know about it
clang -Weverything test_gcc.c
warning: unknown warning option '-Weverything' [-Wunknown-warning-option]
also
clang -Wuninitialized test_gcc.c
does not give a warning
How would I get a warning for an uninitialized variable read in clang 2.9?

Get a newer version of clang; 2.9 is over a year old.

Related

WASI: wasm-ld symbol not found

The following short c example uses the standard c library and therefore requires the wasi sdk:
#include <stdio.h>
int main(void)
{
puts("Hello");
return 0;
}
When compiling the code directly with clang to wasm it works without problem:
clang --target=wasm32-unknown-wasi -s -o example.wasm example.c
My understanding of the LLVM tool chain is that I could achieve the same result with either
clang -> LLVM IR (.ll) -> LLVM native object files (.o) -> convert to wasm
clang -> LLVM native object files (.o) -> convert to wasm
I am able to use the second approach with a simple C program which does not use standard lib calls, when trying with the example above I receive a undefined symbol error:
clang --target=wasm32-unknown-wasi -c example.c
wasm-ld example.o -o example.wasm --no-entry --export-all
wasm-ld: error: example.o: undefined symbol: puts
I do not know if my problem is that I use the wrong clang parameters and therefore not export enough information or that the error is in the wasm-ld command.
Would be happy if someone could give me more insight into tool chain, thanks

The clang compiler does not support a feature it reportedly supports

I'm trying to tailor compiler flags of clang-11 to the apple-m1 CPU, which clang-11 doesn't know about yet.
The output of /usr/bin/clang -E - -mcpu=apple-m1 -### on macOS outputs a command with flags like these in it: "-target-feature" "+zcz"
From that you can infer the following features of the CPU:
armv8.5a+fp-armv8+neon+crc+crypto+dotprod+fp16fml+ras+lse+rdm+rcpc+zcm+zcz+fullfp16+sm4+sha3+sha2+aes
However, out of these, +fp-armv8+neon+zcm+zcz+fullfp16 are not recognised to be valid by any clang compiler:
$ cc -march=armv8.5a+zcz test.c
clang-11: error: the clang compiler does not support '-march=armv8.5a+zcz'
How can I tell clang to optimise for those target flags?

qemu-irix fails to build under Alpine 3.10 (GCC 8.3)

Just for fun, I'm attempting to build the irixxxx's qemu-irix as a Docker image.
It builds successfuly under Debian Buster (using GCC 8 container). However, it fails to build under Alpine 3.10 (it fails only when I selected the Irix targets), with the following errors (at the make stage):
CC util/oslib-posix.o
In file included from util/oslib-posix.c:39:
/usr/include/sys/signal.h:1:2: warning: #warning redirecting incorrect #include <sys/signal.h> to <signal.h> [-Wcpp]
#warning redirecting incorrect #include <sys/signal.h> to <signal.h>
^~~~~~~
CC util/qemu-openpty.o
util/qemu-openpty.c: In function 'qemu_openpty_raw':
util/qemu-openpty.c:123:9: warning: implicit declaration of function 'openpty'; did you mean 'openat'? [-Wimplicit-function-declaration]
if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
^~~~~~~
openat
util/qemu-openpty.c:123:9: warning: nested extern declaration of 'openpty' [-Wnested-externs]
(...)
CC irix-linux-user/linux-user/syscall.o
/qemu-irix/linux-user/syscall.c:6784:22: error: 'F_EXLCK' undeclared here (not in a function); did you mean 'F_RDLCK'?
TRANSTBL_CONVERT(F_EXLCK),
^~~~~~~
/qemu-irix/linux-user/syscall.c:6779:51: note: in definition of macro 'TRANSTBL_CONVERT'
#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
^
/qemu-irix/linux-user/syscall.c:6785:22: error: 'F_SHLCK' undeclared here (not in a function); did you mean 'F_RDLCK'?
TRANSTBL_CONVERT(F_SHLCK),
^~~~~~~
/qemu-irix/linux-user/syscall.c:6779:51: note: in definition of macro 'TRANSTBL_CONVERT'
#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
^
In file included from /qemu-irix/linux-user/syscall.c:121:
/qemu-irix/linux-user/syscall.c: In function 'target_to_host_sigevent':
/qemu-irix/linux-user/syscall.c:7422:27: error: 'struct sigevent' has no member named '_sigev_un'; did you mean 'sigev_value'?
__get_user(host_sevp->_sigev_un._tid, &target_sevp->_sigev_un._tid);
^~~~~~~~~
/qemu-irix/linux-user/qemu.h:501:5: note: in definition of macro '__get_user_e'
((x) = (typeof(*hptr))( \
^
/qemu-irix/linux-user/syscall.c:7422:5: note: in expansion of macro '__get_user'
__get_user(host_sevp->_sigev_un._tid, &target_sevp->_sigev_un._tid);
^~~~~~~~~~
/qemu-irix/linux-user/syscall.c:7422:36: error: '(const bitmask_transtbl *)&<erroneous-expression>' is a pointer; did you mean to use '->'?
__get_user(host_sevp->_sigev_un._tid, &target_sevp->_sigev_un._tid);
^
/qemu-irix/linux-user/qemu.h:501:5: note: in definition of macro '__get_user_e'
((x) = (typeof(*hptr))( \
^
/qemu-irix/linux-user/syscall.c:7422:5: note: in expansion of macro '__get_user'
__get_user(host_sevp->_sigev_un._tid, &target_sevp->_sigev_un._tid);
^~~~~~~~~~
/qemu-irix/linux-user/qemu.h:506:13: warning: left-hand operand of comma expression has no effect [-Wunused-value]
(hptr)), (void)0)
^
/qemu-irix/linux-user/qemu.h:510:31: note: in expansion of macro '__get_user_e'
# define __get_user(x, hptr) __get_user_e(x, hptr, be)
^~~~~~~~~~~~
/qemu-irix/linux-user/syscall.c:7422:5: note: in expansion of macro '__get_user'
__get_user(host_sevp->_sigev_un._tid, &target_sevp->_sigev_un._tid);
^~~~~~~~~~
/qemu-irix/linux-user/syscall.c: In function 'do_syscall':
/qemu-irix/linux-user/syscall.c:13545:25: warning: implicit declaration of function 'sethostid'; did you mean 'gethostid'? [-Wimplicit-function-declaration]
ret = get_errno(sethostid(arg1));
^~~~~~~~~
gethostid
/qemu-irix/linux-user/syscall.c:13545:25: warning: nested extern declaration of 'sethostid' [-Wnested-externs]
make[1]: *** [/qemu-irix/rules.mak:66: linux-user/syscall.o] Error 1
make: *** [Makefile:472: subdir-irix-linux-user] Error 2
The command '/bin/sh -c make && DESTDIR=/tmp/qemu make install' returned a non-zero code: 2
I researched about the differences between Debian's and Alpine's GCC (in fact, libc6 and musl) about similar failures, but I haven't found any information about how to solve this without modifying the code.
So, where can I find more information about this issue and which packages may solve it? Thanks.
Dockerfiles and full log here. Docker container image (Debian Buster) here.
The difference seems to be caused by the underlying libc implementations: glibc of Debian, vs musl-libc of Alpine.
While GNU libc is the defacto standard libc implementation in Linux, musl libc is used by a handful of distributions, such as Alpine Linux and Void Linux. musl is a minimalistic strict-POSIX libc implementation, and is generally not compatible with glibc. Usually, software projects have to be ported to musl libc to be supported on Alpine, especially non trivial applications.
The compilation of syscall.c breaks on several places, the first being:
/qemu-irix/linux-user/syscall.c:6784:22: error: 'F_EXLCK' undeclared here (not in a function); did you mean 'F_RDLCK'?
TRANSTBL_CONVERT(F_EXLCK)
The F_EXLCK is macro is not defined in musl libc's fcntl.h. However, it could be easily patched by defining it manually, for example with make CFLAGS='"-DF_EXLCK=4"'. This is how qemu is patched for musl for non-irix targets (patch link).
However, there are more undefined macros down the road, such as __SIGRTMIN and __SIGRTMAX, the macro TRANSTBL_CONVERT, and probably others. Patching them ad-hoc may not be enough - so it appears to be that the qemu-irix project has to be properly ported for Alpine and musl libc.
If you're willing, you could try following existing qemu musl patches, and attempt to patch it yourself for qemu-irix:
- https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg04773.html
- https://github.com/NixOS/nixpkgs/pull/46449/files
Off topic comment, use make -j to build parallely, which will end much faster.

How / is it possible to build C "Hello world" program entirely static (OS X, Clang)?

Is it possible to compile C "Hello world" program to have final executable entirely static?
#include <stdio.h>
int main() {
printf("hello world!\n");
return 0;
}
I've tried the following:
clang -static main.c
But it gives:
ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've seen staticly linked libc? but I want to get additional information on this topic.
P.S. I am asking this for education purposes rather than for real practice.

Program segfaults when compiled with clang but not gcc

Summary: When I set the -mcmodel=large flag when compiling with clang my application segfaults when accessing thread local storage. This does not happen when compiling with gcc. Is this a bug with clang or something I am doing wrong?
Details:
The following code segment crashes when compiled with clang when setting the -mcmodel flag, but it runs fine when compiled with gcc
#include <stdio.h>
#include <pthread.h>
__thread int tlsTest;
int main(int argc, char **argv) {
printf("&tlsTest is %p\n", &tlsTest);
tlsTest = argc;
printf("tlsTest is %d\n", tlsTest);
return 0;
}
When I compile with: clang test.c -pthread -mcmodel=large the result is:
&tlsTest is 0x7fd24262c6fc
Segmentation fault (core dumped)
But with: gcc test.c -pthread -mcmodel=large the result is:
&tlsTest is 0x7f1cf785c6fc
tlsTest is 1
The program also works fine when compiled with: clang test.c -pthread
I read the following link about mcmodel but I'm not sure how this relates to the segfault that I've observed. Note that the problem occurs for -mcmodel=medium also, but not for -mcmodel=small.
Is this a bug with clang/llvm or is it a different interpretation of the standard or some unimplemented feature?
Also my system is Ubuntu 12.04. My version of gcc is 4.6.3 and the version of clang/llvm that I tested is a recent snapshot of 3.3 development, and I also tested with clang 3.2.

Resources