强制编译未引用的变量 [英] Force compile unreferenced variable

查看:129
本文介绍了强制编译未引用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要强制编译器编译看似无意义的代码块,如下所示:

Hi All

I need to force the compiler to compile a code block that is seemingly meaningless, something like this:

int iEAX = 0x30313233;
int iEBX = 0x31323334;
int iECX = 0x32333435;
int iEDX = 0x33343536;


这些变量将不会在它们下面的代码中的任何地方引用,我只需要对其进行编译.

如您所见,我希望将这些语句转换为类似的内容:mov EAX, 30313233H等,但这要问的太多了,所以如果我只能将这些常量放入OBJ文件,我会很高兴正是在我已经定义了未引用变量的地方,无论它们所涉及的指令是什么.

如果禁用优化很容易,但这不是重点,无论应用了什么优化,都必须编译该块.如果有一个#pragma 可以仅针对该特定点禁用优化,但是没有...(我认为)

您可能会猜到,我需要这些常量来标识OBJ文件中的该点(在编译后),然后将其替换为通过其他方式编译的一些二进制代码.

如果有这种情况,那就可以了:


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



不必完全像这样,关键是这些常量包围的二进制代码.
同样也不必将它们设置为int常量,它们可以是静态字符串,也可以是我可以用来识别内联函数的位置(在OBJ文件中)的任何内容.

我为什么要这么做并不是秘密,我只是想保持TC简短.如果有人好奇,问我,我会告诉你! ;):D

在此先多谢! :)

P.S.我认为人们已经意识到通常会跳过这些障碍.实际上,我做了一切我可能会误导的编译器来编译该bolck和类似于我已经显示的功能,但是它一直跳过它们...:)



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... :)

推荐答案

尝试用volatile关键字声明它们-那应该是一个不错的编译器,然后...:laugh:
Try declaring them with the volatile keyword - It should be a good little compiler then... :laugh:


我找到了完美的解决方案.

我需要做的每一件事是在H文件中将这些变量声明为extern而不给出它们的定义.像这样的东西:

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:

// thest.h

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



然后在CPP文件中的正确位置



then in the right spot in the CPP file

#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;
}



编译器不能跳过这些语句的编译,因为它不知道它们的重要性! ;)无论该语句在哪里存在,都将在其站立"的位置进行编译,如果它是内联函数,则不会跳过整个函数的编译,即使是该功能体内唯一的东西.

现在,当然,这样的解决方案会产生很多链接错误,但我的观点是,到要链接特定的OBJ文件时,这些语句就不会出现了. ;)它们将由不同模块(ml64)生成的实际二进制代码代替...

无论如何,非常感谢您的帖子! :)



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! :)


您可以在代码中定义一个私有段,如下所示:
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.


这篇关于强制编译未引用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆