.text节的开头/结尾有多余的空间 [英] Extra space at the beginning/end of .text section

查看:75
本文介绍了.text节的开头/结尾有多余的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以使用Visual Studio编译器在C/C ++的.text部分的开头/结尾处保留一些额外的空间.

I'm looking for a way to reserve some extra space at the begining/end of the .text section from C/C++ using Visual Studio compiler.

我只找到了一种关于如何在Visual Studio的代码部分中保留一些额外空间的解决方案: PE文件.text节的大小

I've only found one solution on how to reserve some extra space in code section in Visual Studio: PE File .text Section Size

#pragma optimize( "", off )
#define NOP __asm { NOP } ;
#define NOP8 NOP NOP NOP NOP NOP NOP NOP NOP
#define NOP64 NOP8 NOP8 NOP8 NOP8 NOP8 NOP8 NOP8 NOP8 
#define NOP512 NOP64 NOP64 NOP64 NOP64 NOP64 NOP64 NOP64 NOP64
#define NOP4096 NOP512 NOP512 NOP512 NOP512 NOP512 NOP512 NOP512 NOP512
#define NOP32768 NOP4096 NOP4096 NOP4096 NOP4096 NOP4096 NOP4096 NOP4096 NOP4096
void unused_global() { NOP32768 }
#pragma optimize( "", on )

int main() {
   [...]
   unused_global(); // <-- Without this call, compiler doesn't include `unused_global` inside .text section
}

此解决方案的问题是:

  1. 它需要直接调用此函数.这当然会破坏应用程序逻辑.
  2. 不能保证 unused_global 函数将放置在.text部分.
  3. 这太棒了,所以我很想听听更干净的解决方案
  1. It requires a direct call to this function. This of course will break the application logic.
  2. It doesn't guarantee that unused_global function will be placed at the end/beginning of the .text section.
  3. It is quite aweful so I would love to hear a cleaner solution

更新

我找到了1)问题的解决方案.同样,这是一个令人敬畏的技巧:

I've found a solution for 1) problem. Again it is an awefull hack:

int main() {
    volatile bool force_false = false;
    if (force_false) unused_global();
}


推荐答案

在VS 2019中,以下代码分别在代码段的开头和结尾保留4K.

With VS 2019 the following reserves 4K at the beginning and end of the code segment, respectively.

#pragma section(".constext", read)
#pragma section(".xonstext", read)
#pragma comment(linker, "/merge:.constext=.text")
#pragma comment(linker, "/merge:.xonstext=.text")

extern "C" __declspec(allocate(".constext")) const char before[0x1000]{ __COUNTER__ };
extern "C" __declspec(allocate(".xonstext")) const char after[0x1000] { __COUNTER__ };

int main()
{
    return before[0] + after[0] - 1;
}

地图文件确认位置.

Preferred load address is 00400000

Start         Length     Name                   Class
0001:00000000 00001000H .constext               CODE
0001:00001000 00000bc8H .text$mn                CODE
0001:00001bc8 00001000H .xonstext               CODE
0002:00000000 000000b4H .idata$5                DATA
[...]

 Address         Publics by Value              Rva+Base       Lib:Object
[...]
0001:00000000       _before                    00401000     constext.obj
0001:00001000       _main                      00402000 f   constext.obj
[...]
0001:00001bc8       _after                     00402bc8     constext.obj
0002:00000000       __imp__SetUnhandledExceptionFilter@4 00404000     kernel32:KERNEL32.dll
[...]

奇怪的是,优化的编译将来自 main return 识别为普通的 0 ,但不会导致虚拟数组引用得到优化离开.

Rather curiously, the optimized compile recognizes the return from main as a plain 0 but does not cause the dummy arrays references to be optimized away.

PUBLIC  _main
;       COMDAT  _main
_TEXT   SEGMENT
_main   PROC    ; COMDAT
; 14   :        return before[0] + after[0] - 1;
        xor     eax, eax
; 15   : }
        ret 0
_main   ENDP
_TEXT   ENDS

这篇关于.text节的开头/结尾有多余的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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