Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increase binary executable size

Tags:

c

Abstract problem: I have some code in C. After compilation the executable has 604 KB. I'd like it to be bigger, let's say 100 MB.

How to achieve this?

I could declare a string to increase binary size, but is there any more scalable solution. That is I'd like to increase compiled size by N bytes without increasing source code by N bytes.

 char* filler = "filler"; // increases compiled size only by few bytes

Use case: I'm developing firmware, and testing remote firmware upgrade feature. I'd like to see how it will behave when firmware is big and heavy.

like image 831
mjjaniec Avatar asked Apr 20 '17 13:04

mjjaniec


People also ask

Why is my Go binary so big?

in Go 1.2, a decision was made to pre-expand the line table in the executable file into its final format suitable for direct use at run-time, without an additional decompression step. In other words, the Go team decided to make executable files larger to save up on initialization time.

Is compiled code smaller?

typically, compiled code is smaller than the source code it is compiled from.

Why are executables so big?

One reason executables can be large is that portions of the C++ runtime library might get statically linked with your program.

How do I reduce the size of an executable application?

Again go to the "Project Settings", "Link" tab. Type /ALIGN:4096 in the Project Options edit box. Press OK and rebuild your project. The size is further reduced to 3K.


2 Answers

This creates a 100MB executable when compiled with gcc:

#include <stdio.h>
#define SIZE 100000000

char dummy[SIZE] = {'a'};

int main(void){
    dummy[SIZE-1] = '\n';
    if(dummy[0] == 'a')printf("Hello, bloated world");
    return 0;
}

By defining the array outside of main you don't blow the stack. By using the array, gcc doesn't optimize it away.

like image 100
John Coleman Avatar answered Oct 22 '22 02:10

John Coleman


GCC specific variant:

char dummy[100*1024*1024] __attribute__((used)) = { 77 };

Applying the attribute 'used', you do not need to touch it any more to prevent it from being optimised away. Still, an non-all-null initialiser must be applied as in John Coleman's solution.

like image 9
Aconcagua Avatar answered Oct 22 '22 01:10

Aconcagua