首页 > 解决方案 > .text 部分开头/结尾的额外空格

问题描述

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

我只找到了一个关于如何在 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) 问题的解决方案。这又是一个很棒的 hack:

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


标签: c++visual-studiocode-injectionportable-executablesections

解决方案


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

#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
[...]

奇怪的是,优化后的编译将returnfrom识别main为普通的0,但不会导致虚拟数组引用被优化掉。

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

推荐阅读