Building the Linux kernel with clang
lwn.net
lwn.net
This will definitely have a positive effect on code quality.
Not even mentioning it should be a little bit easier to configure IDEs like CLion for working with Linux source tree.
[0] https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.h...
It is developed by google[0] afaik. The link says it is present in LLVM and gcc. I don't know what makes the difference (may be License?). On the contrary I have felt gcc better for my work. I have been following a clang bug[1] for long and have seen no fix so far.
[0] https://github.com/google/sanitizers/wiki/AddressSanitizer
Dynamic alloca overflow detection
Symbol size changing for global variables
Adaptive global redzone sizes
Invalid pointer pairs detection
Support for no_sanitize attribute
Support for dead stripping of globals on Linux
I might be out of date on some or even all, so please correct me where I'm wrong. Thank you in advance.
Address sanitizer was "ported" to the kernel a while ago; not sure if UBSAN is or will be in the future.
Specifically, this sounds like it is just preferring the other toolchain. Are there literally things that don't have equivalents?
That is, my guess is these extensions are the equivalent of Chesterton's fence. They were introduced for a reason. If you have evidence that that reason is no longer apropros, present the evidence. But don't just complain about the fence.
I think it's self-evident that the use of proprietary, nonstandard language extensions is a bad idea. It leads to multi-year efforts getting other compilers to support "features" that aren't in the spec (and as a result, are typically underspeced by the compilers that implement them and require reverse engineering to fully understand and implement correctly). Linux isn't written in C, it's written in some other language that's deceptively similar to C. It should be enough to write a compiler that accurately implements the C standard to compile the kernel with.
Don't get me wrong, I'm highly amenable to this argument, but it is not an evidence based one.
Now, there could be some easy evidence that would help. For example, evidence that kernels which are built using clang build just as easily using gcc. Or icc. Or ___. That would help. Right off, I do not know if that is the case.
- Multi-year effort by Clang, a well regarded standards-compliant C compiler, to support the kernel
- Proprietary extensions do not conform to the same strict guidelines for specifications as the C standard and may require reverse engineering to implement correctly
- C is defined by the C specification and Linux is not written in C by this metric
- Therefore, C compilers cannot compile the kernel
Correct me if I'm wrong but I cannot extract similar facts from your comment.
So my question to you is if you have evidence that those extensions are not required to actually build the kernel? I get that they are the "sticking" point to moving to clang. But are they also required for the kernel to actually do its job?
UNIX, the genesis of C and all that derived from AT&T original code.
> And did it successfully compile with multiple compilers?
Any that was K&R C compatibile.
GCC was a toy compiler not used for any serious UNIX development until Sun decided to start selling the SDK as part of a SunOS developers edition, quickly followed by other UNIX vendors.
So by definition, until GCC became an usefull project, there were no UNIX kernels written with GCC C extensions.
The patches to make it work for icc is simple enough that this is almost a non-issue: https://lists.freebsd.org/pipermail/freebsd-arch/2003-Novemb... (albeit old)
He hates compiler writers being tricky.
In the C community, undefined behavior may be humorously referred to as "nasal demons", after a comp.std.c post that explained undefined behavior as allowing the compiler to do anything it chooses, even "to make demons fly out of your nose".
Flexible array members may only appear as the last member of a struct that is otherwise non-empty.
What is the article referring to, then?
1: https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Zero-Length.htm...
Weird, but I can see the potential utility when you're optimizing for locality in heap allocations, since the allocator will give you a single contiguous memory block with all of the VLAs, while making several allocations for multiple VLAs could very well return disjoint memory ranges. And then there's the aesthetic utility of behaving like objects in higher-level languages, where having multiple variable-length arrays within an object is standard.
See https://stackoverflow.com/questions/14629504/variable-length... and linked pages.
struct { int arr[n]; } s = { 0 };
gives error: variable-sized object may not be initialized
And (struct { int arr[n]; }){ 0 }
gives error: compound literal has variable size
GCC still doesn't even permit a simpler compound literal like (int[n]){ 0 }. I understand that compound literals and VLAs are distinct, and that the above limitations are, strictly speaking, consistent with the standard constructs. But I would think that once you have variable-length struct members that it wouldn't be too difficult to unify the semantics of VLAs and compound literals. If GCC can't be bothered to improve language consistency, then that's pretty strong evidence that VLA struct members are a dead-end and I can't fault clang for avoiding it.But if GCC did improve consistency, it might significantly help push C toward safer array semantics. The last hurdle would be definition scope and working out how declarations worked. Presumably the ABI would require fat pointers that embed VLA annotations. Getting there would be a long, winding road, but this is how it would happen. It's complex, but the cost+benefit ratio would, I think, be better for both compiler writer and programmer compared to the complexity of C++.
void foo (int n) {
struct S { int x[n]; };
}
https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html #include <stdio.h>
void foo (int n) {
struct S { int x[n]; int y; };
printf("offset: %ld\n", __builtin_offsetof(struct S, y));
}
int main() {
foo(2);
foo(16);
}
offset: 8
offset: 64 keno@anubis:~$ cat test2.c
void bar(int *);
int foo(int n) {
struct {
int non_terminal_val[n];
int some_other_member;
} val;
bar(val.non_terminal_val);
return val.some_other_member;
}
keno@anubis:~$ gcc -c test2.c
keno@anubis:~$ clang test2.c
test2.c:4:13: error: fields must have a constant size:
'variable length array in
structure' extension will never be supported
int non_terminal_val[n];
^
1 error generated.Clang don't want to support that GCC extension because it's too ugly.
See, for ex., https://wiki.freebsd.org/BuildingFreeBSDWithClang https://unix.stackexchange.com/questions/49906/why-is-freebs...
> Kroah-Hartman said that he wished there was a competitor to the Linux kernel.
Linux people refuse to look outside their vacuum chamber.
It's about a kernel for which compiling with Clang is (1) interesting and (2) novel. If you know Linux, you'll recognize that's Linux.
What does LWN stand for, anyway?
LWN, initially, was "Linux Weekly News." That name has been deemphasized over time as we have moved beyond just the weekly coverage, and as we have looked at the free software community as a whole. We have yet to come up with a better meaning for LWN, however.
But according to what I see on that page, my interrogation remains : once the kernel is built without gcc, then GNU's power will fade away.
GPL and HTML have, in their own ways, enforced that some important code remains open (and sharable and modifiable). By weakening them, the openness will be weakened too.
Of course it won't be 1000 years of darkness, but well, it makes me a bit sad :-)
It wasn't only due to the better plugin support.