Click here to Skip to main content
15,868,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

I need to force the compiler to compile a code block that is seemingly meaningless, something like this:
C#
int iEAX = 0x30313233;
int iEBX = 0x31323334;
int iECX = 0x32333435;
int iEDX = 0x33343536;

Those variables will not be referenced anywhere in the code below them, I just need them to be compiled.

As you can see I'd like those statements to be translated as something like: mov EAX, 30313233H etc, but that'd be too much to ask so I'd be happy if I can only get those constants into OBJ file exactly at the spot I've defined the unreferenced variables regardless of the instructions they are involved in.

It's easy if I disable the optimizations but that's not the point, that block has to be compiled regardless of the optimizations applied. If there was a #pragma that can disable the optimizations just for that specific spot, but there isn't... (I think)

You might guess that I need those constants to identify that spot in the OBJ file (after it gets compiled) and replace them with some binary code compled by other means.

That recuires if I have this case:
inline void func(int i)
{
      int uv1 = 0x12345678;
      int uv2 = i;
      int uv3 = 0x87654321;
}

It has to be translated as something like:
mov EAX, 12345678H
mov EBX, dowrd ptr [ESP + "offset"] ; that's the formal argument 'i'
mov ECX, 87654321H


It doesn't have to be exactly like this, the point is the binary code to be surrounded by these constants.
Also it is not necessary those to be int constants, they could be static strings or whatever I can use to identify the spot where the function was inlined (in the OBJ file).

It's not secret why I want this, I just want to keep the TC short. If somebody is curious just ask me I'll tell you! ;) :D

Thanks a lot in advance! :)

P.S. I assume people are aware of that those blocks would normaly be skipped. Actually I did everything I could get to mislead the compiler to compile that bolck and a function similar to that I've shown but it keeps skipping them... :)
Posted

Try declaring them with the volatile keyword - It should be a good little compiler then... :laugh:
 
Share this answer
 
Comments
Ivan Ivanov 83 27-Dec-10 10:35am    
Yes! It works for the code block (in the middle of a larger function) but not for the inline function.. good idea BTW :)
I found the perfect solution.

Everyting I needed to do is to declare those variables as extern in a H file without giving them definitions. something like this:

C++
// thest.h

extern int iEAX;
extern int iEBX;
extern int iECX;
extern int iEDX;


then in the right spot in the CPP file
C++
#include "thest.h"

void ImportantFunction()
{
	// The spot that needs to be identified in the corresponding OBJ file
	iEAX = 0x30313233;
	iEBX = 0x31323334;
	iECX = 0x32333435;
	iEDX = 0x33343536;
}


The compiler cannot afford to skip the compilation of those statements because it doesn't know of what importance they are! ;) No matter where such statement exists it will be compiled at the very spot it "stands", if it's in an inline function the compilation of the entire function won't be skipped as well, even if the that's the only thing in that function's body.

Now, of course such a solution would produce a lot of link errors, but my point is that by the time the particular OBJ file is to be linked those statements won't be there. ;) They would be replaced by actual binary code generated by different module (ml64)...

Thanks a lot for the posts anyway! :)
 
Share this answer
 
you can define a private segment in your code like this:
#pragma data_seg( "identifyme" )
static int iEAX = 0x30313233;
static int iEBX = 0x31323334;
static int iECX = 0x32333435;
static int iEDX = 0x33343536;
#pragma data_seg()

so its easier for you to find it in your module too.
 
Share this answer
 
Comments
Ivan Ivanov 83 30-Dec-10 7:52am    
I greatly appreciate your answer and I really wish it would work, but it doesn't unfortunately.
You see a definition of a data/code segment isn't relative to the context in which the very definition is placed in. It doesn't matter wheter you define it inside of a fuction or in the open area (for instance in the beginning of a file). It'll place the code in a speciffic location in the OBJ (and later in the executable) not right in the place of data/code_seg definition ;).
Thanks a lot anyway! :)
OK I'm almost there, the solution for identifying a block within a larger blck is already found! Now the soultion for inline function. An identifying code that will be placed wherever the function is inlined. - That's a hard stuff as it seems! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900