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
Thread Post new topic Reply to topic
guignol



Joined: 06 Dec 2008
Posts: 763
guignol 13 Apr 2021, 21:38
rugxulo wrote:
And it takes effort to simplify things.
leave the effort to an AI
Post 13 Apr 2021, 21:38
View user's profile Send private message Reply with quote
MarcoV



Joined: 28 Apr 2019
Posts: 7
MarcoV 30 Sep 2021, 20:23
vivik wrote:

>Free Pascal
how good is it? Probably doesn't support SEH?


It does for 64-bit since version 3.0.0 or even earlier(2015), and 32-bit since 3.2.0 (2020)

The reason for the difference is afaik that on 64-bit calling SEH code broke if the calling code didn't, so it was a must. Also when the original 32-bit exception code was written (1999 or thereabouts) the patents on table driven exceptions were still active.
Post 30 Sep 2021, 20:23
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3913
Location: vpcmipstrm
bitRAKE 01 Oct 2021, 00:04
I've been seeing:
Code:
push byte 0xXX
pop reg    
... for size optimized code, so there is hope that compiler writers will take these things more serious.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 01 Oct 2021, 00:04
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1586
Location: Toronto, Canada
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.
Post 09 Oct 2021, 23:48
View user's profile Send private message Send e-mail Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2418
Furs 10 Oct 2021, 13:08
Yeah, not using the standard library is the way to go.
Post 10 Oct 2021, 13:08
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
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!".

Image
Post 29 Jun 2023, 15:35
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
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.cSad.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.cSad.text+0x18): undefined reference to `printf'
collect2: error: ld returned 1 exit status
boo@DESKTOP-1V5DHQJ:/mnt/c/Users/BOO/Projects$    
Post 29 Jun 2023, 15:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
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.
Post 29 Jun 2023, 15:43
View user's profile Send private message Visit poster's website Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 29 Jun 2023, 15:47
revolution wrote:
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.


I see, thanks for your swift reply. Very Happy
Post 29 Jun 2023, 15:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
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
Post 29 Jun 2023, 15:54
View user's profile Send private message Visit poster's website Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 29 Jun 2023, 15:55
revolution wrote:
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.


Still fails.

C code from: https://jameshfisher.com/2018/02/19/how-to-syscall-in-c/

Image
Post 29 Jun 2023, 15:55
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 29 Jun 2023, 15:56
Ah, I just saw your reply.
Post 29 Jun 2023, 15:56
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2418
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).
Post 29 Jun 2023, 17:26
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1586
Location: Toronto, Canada
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!
Post 29 Jun 2023, 17:41
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
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.
Post 29 Jun 2023, 17:49
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
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    
I gues one can put these in the Makefile or build script when creating the libraries and executables.

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.
Post 30 Jun 2023, 10:34
View user's profile Send private message Visit poster's website Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
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.
Code:
-ffunction-sections -fdata-sections -fno-strict-aliasing    
I gues one can put these in the Makefile or build script when creating the libraries and executables.

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.


I tried the command-line switches, compiles okay, but no changes to size of the program.


Description:
Filesize: 24.2 KB
Viewed: 2468 Time(s)

Screenshot 2023-06-30 190239.png


Post 30 Jun 2023, 11:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
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.
Post 30 Jun 2023, 11:05
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1586
Location: Toronto, Canada
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!
Post 30 Jun 2023, 11:47
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20067
Location: In your JS exploiting you and your system
revolution 30 Jun 2023, 11:53
AsmGuru62 wrote:
So, the library has to be 'built' with the feature to use functions separately.
Yes.

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.
Post 30 Jun 2023, 11:53
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.