Hello,
I'm trying to create a data area within an .exe file. Then I will open the .exe file and scan it for the data area. When the data area is found, I will write some data to this data area and write the .exe file back to disk. I want to accomplish this by creating a character array in my source file like so:

char arr[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

Then that should be stored in the .exe file somewhere. The problem is, all of those A's don't appear anywhere in the .exe file . I know why, it's because the compiler ignored the array because I didn't reference it. If I reference it, by using a simply for loop and incrementing each element of the array, then those A's do appear in the .exe file as expected.

So, my question is, is there anyway to keep the compiler from deciding for me what I need or don't need in my code? So that I don't have to reference the array in order to get it included in the compilation process? I thought maybe this problem had to do with the compiler's optimization but I turned off the optimization feature in the compiler's configuration but with no luck. I'm using Visual Studio Community 2015. Any help would be appreciated.
Thanks.

This sounds a lot like a means to pwn an application. Please take your attempts to subvert application security elsewhere!

Comments
You are wrongly judging me. I'm not trying to do anything illegal or immoral. I just thought that there might be a means provided within the compiler

Could indeed be a security issue, but I know patching was used in the previous century to fix or improve or modify software backed into ROM. Don't ask me how it was done.

Ok. Assuming I have misjudged you, a lot of compilers these days, such as Visual Studio, employ what is called something like address randomization in order to make it more difficult to corrupt software. IE, the address of something like your array will not be at the same relative location every time it is compiled or run. You might be able to insert a tag in the code that will allow you to pinpoint where your target address really is. IE, doing something like allocating a bit more space for the array, and then insert the tag at the end of the array. Where you find your tag, you can then compute there the beginning of the array would be. Let's say you want a char array of 100 bytes (including terminating null byte), and you are using a tag of "xyzzy". Then, allocate an array of 106 bytes and append the string "xyzzy" (including terminating null byte) at &array[100]. I haven't done this myself, so this is a "theoretical" approach, but it should work.

To continue, in your example, you have defined an unused string on the stack. The compiler will, optimizations notwithstanding, not include it in the image. You need to do something with it in your code. Also, it is on the stack. Put it on the heap in an extern variable using a malloc/calloc or strdup call, and then do something with it. You might be able to declare it as an extern without allocating the space with the code you show, but inside a function it is a stack-based variable and therefore not accessible until the function is called, but the fact that the compiler will remove it since it is unused, this is where your problem resides. Have fun!

This article has been dead for over six months. Start a new discussion instead.