Increase executable bean size

Abstract problem: I have code in C. After compilation, the executable has 604 KB. I would like it to be larger, say 100 MB.

How to do it?

I can declare a string to increase binary size, but is there an even more scalable solution. That is, I would like to increase the compiled size by N bytes without increasing the source code by N bytes.

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

Use case: I am developing firmware and testing a remote firmware update. I would like to see how he will behave when the firmware is large and heavy.

+7
c
source share
3 answers

This creates a 100 MB 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 an array outside main , you will not explode the stack. Using an array, gcc does not optimize it.

+13
source share

Specific GCC Option:

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

Using the used attribute, you no longer need to touch it to prevent it from being optimized. However, not all zeros initializer should be applied, as in John Coleman's decision.

+8
source share

You will need to create a global array with all elements explicitly initialized. Elements must be randomized, otherwise the compiler will probably optimize the list of initializers in the compiled code.

First you need a separate program to generate your array:

 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int len = 100000000; int i; srand(getpid()); printf("unsigned char buf[%d] = {\n", len); for (i=0;i<len;i++) { printf(" %hhu,", rand() & 0xff); if (i%16==15) printf("\n"); } printf("};\n\n"); return 0; } 

Run this and redirect the output to a file:

 ./array_generator > array.c 

Then you get a .c array that looks something like this:

 unsigned char buf[1000000] = { 247, 223, 30, 51, 46, 247, 133, 136, 254, 225, 82, 135, 68, 176, 240, 7, 29, 245, 104, 203, 230, 83, 127, 189, 37, 5, 168, 105, 134, 9, 229, 125, 232, 3, 176, 23, 251, 53, 159, 249, 22, 241, 128, 90, 161, 112, 97, 191, 101, 202, 138, 75, 29, 10, 9, 66, 15, 177, 171, 149, 186, 145, 18, 163, ... }; 

Then you include this in your main source:

 #include "array.c" 
+1
source share

All Articles