链接描述文件中的“.= 0x7c00”到底是做什么的?

What exactly does ". = 0x7c00" in a linker script do?

链接描述文件中的“.= 0x7c00”究竟有什么作用?

更具体地说,当我将 . = 0x7c00 放在链接描述文件的开头时,为什么生成的输出文件不以 0x7c00 = 31,744 个零开头?

我了解到当 PC 启动时,BIOS 将 512 字节的 MBR 放在内存地址 0x7c00 处。但是,我对链接器的位置计数器究竟如何影响输出文件的布局感到困惑。

(对于上下文,我试图彻底理解 "x86 bare metal" 项目中的示例代码。https://github.com/cirosantilli/x86-bare-metal-examples。我在下面包含了整个链接器脚本作为上下文。)

SECTIONS
{
    /*
    We could also pass the -Ttext 0x7C00 to as instead of doing this.

    If your program does not have any memory accesses, you can omit this.
    */
    . = 0x7c00;
    .text :
    {
        __start = .;

        /*
        We are going to stuff everything
        into a text segment for now, including data.
        Who cares? Other segments only exist to appease C compilers.
         */
        *(.text)

        /*
        Magic bytes. 0x1FE == 510.

        We could add this on each Gas file separately with `.word`,
        but this is the perfect place to DRY that out.
        */
        . = 0x1FE;
        SHORT(0xAA55)

        *(.stage2)

        __stage2_nsectors = ABSOLUTE((. - __start) / 512);

        . = ALIGN(512);
        __end = .;
        __end_align_4k = ALIGN(4k);
    }
}

看来". = 0x7c00"不是长度而是绝对地址。它对我来说是“设置特殊变量”的当前值“。”成为十六进制值 0x7c00 然后它计划稍后在脚本中使用该地址作为偏移量,就像 . = ALIGN(512) 这也是为什么它将该地址保存为 __start 以便它可以进行数学计算在生成的图像上。如果您在脚本期间操作 . 使其指向添加到图像的最后一块内存,那么您可以使用它来确定总大小:

__stage2_nsectors = ABSOLUTE((. - __start) / 512);

用英语是

起点和终点的差值除以扇区大小。