-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't catch exceptions at compile time with --os:standalone #18322
Comments
Why use |
Are there ways to achieve what I want with |
I don't know, it's 2021 and I expect linkers to be able to perform dead code elimination, including for C's bizzarely bloated builtin IO facilities. |
I think the linkers are doing their job fine. It's just if there's a single printf or puts somewhere it pulls in a bunch of other code. |
I compiled following simple C code with several gcc options to see if gcc's linker can remove unused C standard library code. #define WIN32_LEAN_AND_MEAN
#include <Windows.h>
int main() {
/*void __main() {*/
MessageBoxA(0, "test", NULL, MB_OK);
return 0;
}
This is gcc commands and sizeof generated executable file:
I changed above C code so that it can be compiled without linking standard C library: #define WIN32_LEAN_AND_MEAN
#include <Windows.h>
/*int main() {*/
void __main() {
MessageBoxA(0, "test", NULL, MB_OK);
/*return 0;*/
} gcc -nostartfiles testc.c
6,211 bytes
gcc -s -nostartfiles testc.c
3,584
gcc -Os -s -nostartfiles testc.c
3,584
gcc -c testc.c && gcc -s testc.o -nostdlib -luser32
3,584
gcc -c testc.c && gcc testc.o -nostdlib -luser32
6,211
gcc -Os -c testc.c && gcc -Os -s testc.o -nostdlib -luser32
3,584
gcc -Os -flto -c testc.c && gcc -Os -flto -s testc.o -e __main -nostdlib -luser32
3,584
gcc -c testc.c -ffunction-sections -fdata-sections && gcc -s testc.o -nostdlib -luser32 -Wl,--gc-sections
3,584 GCC options: -nostartfiles: Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib, -nolibc, or -nodefaultlibs is used. -nostdlib: Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored. gcc can generate minimum executable file from the minimum C code only with |
Thanks for looking into this. Executable size and dead code aren't exactly the problem - my target platform has plenty of ROM but not much RAM. I don't want to remove the C stdlib, I just need to avoid the I/O functions which bring in some large statically-allocated arrays. An empty program using This means I can make However I do wonder if there's still a use case for |
There is also |
I'm having this exact same issue. I'm trying to write a kernel in Nim and I'm starting with a UEFI bootloader. I was able to get everything working with If I try to switch to Is there a way to avoid the dependency on those stdio routines in |
|
Good to know! I actually got myself unblocked earlier by providing stubs to the missing functions (they weren't being called anyway). I also realized that I need |
The combination of
--gc:arc --os:standalone
is viable now thanks to #16404 being fixed in devel. However, any code that relies on catching exceptions at compile time will no longer compile. This breaks a lot, for example I can't usefmt
in macros anymore.Example
Current Output
Expected Output
Program compiles successfully.
Additional Information
--os:standalone
seems to be the culprit, it happens regardless of--gc
setting.The text was updated successfully, but these errors were encountered: