What are your opinions about a explicitly None or BareMetal OSType? (llvm::Triple::OSType)
Background
I wanted to compile some code with clang on Windows for amd64-none-elf target, which unexpectedly failed do to this issue.
Possible solutions
Introduce None as an explicit OSType: this results in some failed tests without special casing some weird combinations. (e.g.: this would fail, as it arm-none-eabi would mean arm-unknown-none-eabi instead of the current arm-none-unknown-eabi)
Introduce BareMetal or FreeStanding as an OSType, as in amd64-baremetal-elf or amd64-freestanding-elf. This has the advantage that it wouldn’t break any existing use cases with the disadvantage that this is not existing practice.
What’s the compiler and linker behaviors of baremetal/freestanding? Is there a specification? Do different architectures have different baremetal behaviors?
If you want to Linux like behavior without using gcc for the link action, you may use -nostdlib (e.g. clang --target=x86_64-pc-linux-gnu -fuse-ld=lld -nostdlib).
I haven’t done extensive testing but I have tried the workaround of using linux-elf and nostdlib. For whatever reason this results in executables that are notably larger than if it were compiled and linked as bare metal (using @Templar-von-Midgard 's branch), although it does work.
One example of a bare metal elf would be a multiboot compliant kernel, for which there is a specification, other boot protocols such as stivale can also use bare metal elf files
Comparing llvm-readelf -S output will be useful to know why the *-linux-gnu one is larger.
It may just have some different defaults, and shall not be difficult to make smaller.
One example of a bare metal elf would be a multiboot compliant kernel, for which there is a specification, other boot protocols such as stivale can also use bare metal elf files
I guess multiboot specification can be considered too specific to be defined as the “baremetal OS type”. Without a clear one, I’d avoid defining a baremetal OS type.
I took a look at it, so it seems like the following two command lines would be similar: clang++ -fuse-ld=lld --target=amd64-pc-baremetal-elf -nostdlib main.cpp clang++ -fuse-ld=lld --target=amd64-pc-linux-elf -static -nostdlib -Xclang -funwind-tables=0 main.cpp
So there are some subtle differences here.
Also, conceptually, it is somewhat counterintuitive and just weird compiling a custom (ie. not linux) kernel with amd64-linux-elf.
Without -nostdlib the the crt objects are also added to the linker invocation (crt1.o, …).