c++ - .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
}
这个解决方案的问题是:
- 它需要直接调用此函数。这当然会破坏应用程序逻辑。
- 它不保证
unused_global
函数将放置在 .text 部分的末尾/开头。 - 这太棒了,所以我很想听听更清洁的解决方案
更新
我找到了 1) 问题的解决方案。这又是一个很棒的 hack:
int main() {
volatile bool force_false = false;
if (force_false) unused_global();
}
解决方案
对于 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
from识别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
推荐阅读
- tensorflow2.0 - 每个循环的张量流警告,跟踪是昂贵的,过多的跟踪可能是由于
- mfc - 在嵌套选项卡结构中显示相同的对话框两次
- java - 将新实例分配给实例变量的变化?
- javascript - MutationObserver 未检测到高度变化
- python - 图像分类模型适用于 32x32 图像,但不适用于 64x64
- ssh - 错误权限被拒绝(公钥、gssapi-keyex、gssapi-with-mic)
- javascript - 来自 API 的 fetch() 数据和来自 API 的 get() 数据是否相同?
- c# - 我在将数据从 XML 文件移动到具有 CDATA 节点类型的 ARRAY 时遇到问题
- reactjs - 将快递与护照整合在一起的麻烦
- python - How to concatenate tensor to another list of tensor in pytorch?