如何为PE部分指定自定义部分起始地址?

时间:2013-09-12 16:20:52

标签: windows linker portable-executable

在Linux上,链接时我可以指定一个部分的任何虚拟地址:

ld -Ttext 0x10000000 -Tdata 0x20000000 foo.o -o foo

但我没有看到Windows的link.exe这样的选项。

是否有可能以某种方式指定PE部分起始地址?

2 个答案:

答案 0 :(得分:1)

MinGW ld可以将这些部分放在任意地址。 Dumpbin和disassemblers可以毫无问题地处理它。

但似乎Windows不接受除默认地址之外的任何内容:如果您尝试将其设置为其他值,Windows将说“不是有效的Win32应用程序”。

基地址必须是0x400000或0x1000000。

.text部分必须是0x401000或0x1001000。

这些部分之间似乎也没有间隙。如果我尝试将.data部分放到0x403000而不是0x402000,那么Windows无法加载它......

(我可能错了,或者mingw ld是马车......)

答案 1 :(得分:0)

使用GCC时,此页面说明如何在绝对地址定义变量(包括提及它们应驻留的部分):https://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/

  

我在这里使用的想法是将变量放在一个特殊的部分   name,然后将其放在链接器文件中的绝对地址。

unsigned char __attribute__((section (".myBufSection"))) buf[128]
__attribute__ ((aligned (512)));
     

有了这个,我的变量将被放入一个名为的部分   '.myBufSection',它将在512地址边界上对齐。   下一步是将该部分放在链接器文件中的地址。

SECTIONS
{
  /* placing my named section at given address: */
  .myBufBlock 0x20000000 :
  {
    KEEP(*(.myBufSection)) /* keep my variable even if not referenced */
  } > m_data

  /* other placements follow here... */
}

PS:How can I declare a variable at an absolute address with GCC?

中提到了另一种方法

PS 2:另一个相关的(未答复的)问题是:How to place a variable at a given absolute address in memory (with Visual C++)