在源码中指定PE节

在源码中指定PE

Win32 ASM 在定义段时指定节:

.386
.model stdcall, flat
option casemap:noneinclude windows.inc
include kernel32.inc
include user32.incincludelib kernel32.lib
includelib user32.libsfdata segment		;指定节名为 'sfdata'
string	db	'hello world!', 0
sfdata ends sfcode	segment		;指定节名为 'sfcode'
start:mov eax, offset stringinvoke MessageBox, 0, eax, eax, MB_OKret
sfcode endsend	start

编译之后,用工具查看生成的节:

CPP中指定节:

//指定节为"mysec",属性为可读可写
//在"..\Microsoft Visual Studio 9.0\VC\crt\src\sect_attribs.h"中,
//出现含有'$'的节名,如".CRT$XIZ", 
//其中.CRT是真正节名, XIZ只是用于obj文件
#pragma section("mysec",read,write)		//把buf存放在"mysec"节中
__declspec(allocate("mysec"))
char buf[] = "hello world!";//如果未引用节中的数据,PE中可能不生成相应的节,数据可以在main中引用
int main() {}

可以用下面指令将两个节合并,合并之后,取后者节名:

#pragma comment(linker"/merge:mysec=.data") //合并后,使用.data做节名

mysec 被合并之后: