确切是什么.= 0x7c00";在链接脚本中做什么? [英] What exactly does ". = 0x7c00" in a linker script do?

查看:82
本文介绍了确切是什么.= 0x7c00";在链接脚本中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接描述文件中的.= 0x7c00"到底有什么作用?

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

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

More specifically, when I place . = 0x7c00 at the beginning of a linker script, why doesn't the resulting output file begin with 0x7c00 = 31,744 zeros?

我知道PC启动时,BIOS会将512字节的MBR放在内存地址0x7c00.但是,对于链接器的位置计数器到底如何影响输出文件的布局方式,我感到困惑.

I understand that when a PC boots, the BIOS places the 512 byte MBR at memory address 0x7c00. However, I am confused as to how exactly the linker's location counter affects how the output file is laid out.

(出于上下文,我试图彻底理解"x86裸机"项目中的示例代码. https://github.com/cirosantilli/x86-bare-metal-examples .出于上下文的考虑,我在下面包括了整个链接描述文件.)

(For context, I'm trying to thoroughly understand the sample code from the "x86 bare metal" project. https://github.com/cirosantilli/x86-bare-metal-examples. I've included the entire linker script below for context.)

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 的原因,这样它就可以对生成的图像进行数学运算.如果在脚本执行期间操作.,使其指向添加到映像的最后一块内存,则可以使用它来确定总大小:

It looks like the ". = 0x7c00" is not meaning a length but an absolute address. It reads to me as 'set the current value of the special variable "." to be the hex value 0x7c00 and then it plans to use that address as an offset later in the script like with the . = ALIGN(512) it is also why it saves that address off as __start so it can then do math on the resulting image. If you manipulate . during the script so it points to the last chunk of memory added to the image then you can use it to determine the total size:

__ stage2_nsectors = ABSOLUTE((.-__start)/512);

英语为

起点和终点之间的差异除以扇区大小.

The difference between the starting place and wherever I ended divided by sector size.

这篇关于确切是什么.= 0x7c00";在链接脚本中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆