.text节的开头/结尾处的多余空格

问题描述 投票:0回答:1

我正在寻找一种使用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)问题的解决方案。同样,这是一个令人敬畏的技巧:

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


c++ visual-studio code-injection portable-executable sections
1个回答
0
投票

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

奇怪的是,优化的编译将return中的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
© www.soinside.com 2019 - 2024. All rights reserved.