flat assembler
Message board for the users of flat assembler.
Index
> High Level Languages > Why C++ programs are so big? Goto page Previous 1, 2, 3 Next |
Author |
|
guignol 13 Apr 2021, 21:38
rugxulo wrote: And it takes effort to simplify things. |
|||
13 Apr 2021, 21:38 |
|
bitRAKE 01 Oct 2021, 00:04
I've been seeing:
Code: push byte 0xXX
pop reg _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
01 Oct 2021, 00:04 |
|
AsmGuru62 09 Oct 2021, 23:48
I just coded a very small utility (using C++ classes of course) in Visual Studio 2019 -- 11Kb (in Release build). I used /NODEFAULTLIB, but I had to code some string functions myself.
|
|||
09 Oct 2021, 23:48 |
|
Furs 10 Oct 2021, 13:08
Yeah, not using the standard library is the way to go.
|
|||
10 Oct 2021, 13:08 |
|
Flier-Mate 29 Jun 2023, 15:35
How to make C program smaller in Linux?
I use "-nostdlib" switch but got error. Now it is 16KB just to print "Hello, World!". |
|||
29 Jun 2023, 15:35 |
|
Flier-Mate 29 Jun 2023, 15:41
I tried -nodefaultlibs and -nostdlib in gcc, but got errors:
Code: boo@DESKTOP-1V5DHQJ:/mnt/c/Users/BOO/Projects$ gcc h.c -nodefaultlibs -o h /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x21): undefined reference to `__libc_start_main' /usr/bin/ld: /tmp/ccXIhnbE.o: in function `main': h.c.text+0x18): undefined reference to `printf' collect2: error: ld returned 1 exit status boo@DESKTOP-1V5DHQJ:/mnt/c/Users/BOO/Projects$ gcc h.c -nostdlib -o h /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000001030 /usr/bin/ld: /tmp/ccAjJnQW.o: in function `main': h.c.text+0x18): undefined reference to `printf' collect2: error: ld returned 1 exit status boo@DESKTOP-1V5DHQJ:/mnt/c/Users/BOO/Projects$ |
|||
29 Jun 2023, 15:41 |
|
revolution 29 Jun 2023, 15:43
printf is part of the stdlib.
If you want no stdlib then you have to avoid printf. You'll need to use the system calls directly. |
|||
29 Jun 2023, 15:43 |
|
Flier-Mate 29 Jun 2023, 15:47
revolution wrote: printf is part of the stdlib. I see, thanks for your swift reply. |
|||
29 Jun 2023, 15:47 |
|
revolution 29 Jun 2023, 15:54
Something like this.
Code: //bin/true ' /*' NAME="${0%.*}" gcc -nostdlib -m32 -DBITS=32 -x c -masm=intel -o "$NAME" "${0}" || exit $? exit 0 */ #include <unistd.h> // STDOUT_FILENO #include <sys/syscall.h> // SYS_write, SYS_exit #define S_(v) #v #define S(v) S_(v) // convert macro into literal string const char text[] = "Hello from "S(BITS)"-bit world!\n"; int main() { asm ( " _start: \n" " mov eax, "S(SYS_write)" \n" " mov ebx, "S(STDOUT_FILENO)" \n" " lea ecx, [%[str]] \n" " mov edx, %[len] \n" " int 0x80 \n" " mov eax, "S(SYS_exit)" \n" " xor ebx, ebx \n" " int 0x80 \n" : : [str] "m" (text), [len] "i" (sizeof (text)-1) ); } Last edited by revolution on 29 Jun 2023, 15:59; edited 2 times in total |
|||
29 Jun 2023, 15:54 |
|
Flier-Mate 29 Jun 2023, 15:55
revolution wrote: printf is part of the stdlib. Still fails. C code from: https://jameshfisher.com/2018/02/19/how-to-syscall-in-c/ |
|||
29 Jun 2023, 15:55 |
|
Flier-Mate 29 Jun 2023, 15:56
Ah, I just saw your reply.
|
|||
29 Jun 2023, 15:56 |
|
Furs 29 Jun 2023, 17:26
Maybe you can also import libc as a normal library and use its printf. Not sure if it needs some initialization, but on Windows it works (treating msvcrt as a normal library, which you'll have to explicitly link to of course).
|
|||
29 Jun 2023, 17:26 |
|
AsmGuru62 29 Jun 2023, 17:41
I always wondered why the whole stdlib needs to be linked into EXE file, why not take only printf plus dependencies for just printf?
FASM does it perfectly with proc/endp macros -- if I have a large ASM file with 50 functions defined with proc/endp and then I use one or two functions from this file -- only these two functions will be inside my EXE file. So, FASM can do it, but compilers from 2020s can't? Weird! |
|||
29 Jun 2023, 17:41 |
|
revolution 29 Jun 2023, 17:49
It can be set to exclude unused functions. There is a compiler switch to make each function a separate section in the lib file during the building phase.
Many libraries that target embedded use the setting to reduce the final output size. It just requires a small amount of forethought to enable it. AFAICT it isn't on by default. |
|||
29 Jun 2023, 17:49 |
|
revolution 30 Jun 2023, 10:34
From what I can find you can use these settings in GCC to enable a build to exclude unused functions.
Code: -ffunction-sections -fdata-sections -fno-strict-aliasing Other compilers probably have similar options or settings. Note that this often prohibits more fine tuned optimisations, so using settings like these might not always be beneficial. As with everything, test it before committing to using it. |
|||
30 Jun 2023, 10:34 |
|
Flier-Mate 30 Jun 2023, 11:03
revolution wrote: From what I can find you can use these settings in GCC to enable a build to exclude unused functions. I tried the command-line switches, compiles okay, but no changes to size of the program.
|
||||||||||
30 Jun 2023, 11:03 |
|
revolution 30 Jun 2023, 11:05
You have to apply it to the library you are linking to. So rebuild the library with those settings.
Your single function "main" can't get any benefit from those settings. |
|||
30 Jun 2023, 11:05 |
|
AsmGuru62 30 Jun 2023, 11:47
So, the library has to be 'built' with the feature to use functions separately.
I wonder if, say, MSVCRT as a golden standard has this feature 'built' into it. I think not, because if I use only 'qsort' and 'bsearch' -- I still get around 47K for my EXE, no matter the compiler switches. No way these two functions are that big. These compilers work together with Anti-Virus manufacturers! If EXE is too small -- suspect it as malware -- then prove it by doing some signature check! |
|||
30 Jun 2023, 11:47 |
|
revolution 30 Jun 2023, 11:53
AsmGuru62 wrote: So, the library has to be 'built' with the feature to use functions separately. Linkers can only link sections, not functions. So if you have each function alone in its own section of the object file then the linker can omit unused functions. |
|||
30 Jun 2023, 11:53 |
|
Goto page Previous 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.