2017-04-11 36 views
0

链接描述文件中的“。= 0x7c00”究竟做了什么?链接器脚本中的“。= 0x7c00”究竟做了什么?

更具体地说,当我把. = 0x7c00在链接脚本的开始,为什么不将生成的输出文件与0x7c00 = 31,744零开始?

据我所知,当电脑启动时,BIOS会将512字节的MBR放在内存地址0x7c00处。但是,我很困惑链接器的位置计数器如何影响输出文件的布局。

(对于背景下,我想从“86裸机”项目彻底了解示例代码。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); 
    } 
} 

回答

1

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

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

在英语中是

的区别在起始地点和结束位置之间按行业大小划分。