以下汇编代码的含义,求助

发布于 2024-10-20 02:45:45 字数 3674 浏览 2 评论 0原文

以下代码来自u-boot:

        /* Initialize GOT pointer.
        ** Global symbols can't be resolved before this is done, and as such we can't
        ** use any global symbols in this code.  We use the bal/ move xxx,ra combination to access
        ** data in a PC relative manner to avoid this.  This code will correctly set the
        ** gp regardless of whether the code has already been relocated or not.                                                          
        ** This code determines the current gp by computing the link time (gp - pc)                                                      
        ** and adding this to the current pc.                                                                                            
        ** runtime_gp = runtime_pc + (linktime_gp - linktime_pc)                                                                         
        ** U-boot is running from the address it is linked at at this time, so this                                                      
        ** general case code is not strictly necessary here.                                                                             
        */                                                                                                                               

/*Branch and link to get current PC in ra */                                                                                     
        bal     1f                                                                                                     
        nop                                                                                               
        .extern _GLOBAL_OFFSET_TABLE_                                            
        .word   _GLOBAL_OFFSET_TABLE_  /* This contains the linked address of the GOT */                                                 
        /* The ra register now contains the runtime address of the above memory location */                                              

        .word   . - 4                  /* This contains the link time address of the previous word,                                      
                                        which is also what the link time expected PC value is */                                         
1:                                                                                                
        move    gp, ra    /* Move current PC into gp register */                                                                         
        lw      a5, 0(ra) /* Load linked address of the GOT into a5 */                                                                   
        lw      a6, 4(ra) /* Load the link time address of the GOT storage location into a6 */                                           
        sub     a5, a6    /* Subtract a6 from t1. */                                                                                     
        /* a5 now contains the difference between the link-time GOT table address and the link time expected PC */                       

        /* Add this difference to the current PC (copied into gp above) so that gp now has the current runtime                           
        ** GOT table address */                                                                                                          
        daddu   gp, a5  # calculate current location of offset table              

我不明白上面的3条指令:

.extern _GLOBAL_OFFSET_TABLE_
.word _GLOBAL_OFFSET_TABLE_ //what the meaning after declaring it as a external symbol?
.word . -4  //and also this one?

TIA

The following piece of code is from u-boot:

        /* Initialize GOT pointer.
        ** Global symbols can't be resolved before this is done, and as such we can't
        ** use any global symbols in this code.  We use the bal/ move xxx,ra combination to access
        ** data in a PC relative manner to avoid this.  This code will correctly set the
        ** gp regardless of whether the code has already been relocated or not.                                                          
        ** This code determines the current gp by computing the link time (gp - pc)                                                      
        ** and adding this to the current pc.                                                                                            
        ** runtime_gp = runtime_pc + (linktime_gp - linktime_pc)                                                                         
        ** U-boot is running from the address it is linked at at this time, so this                                                      
        ** general case code is not strictly necessary here.                                                                             
        */                                                                                                                               

/*Branch and link to get current PC in ra */                                                                                     
        bal     1f                                                                                                     
        nop                                                                                               
        .extern _GLOBAL_OFFSET_TABLE_                                            
        .word   _GLOBAL_OFFSET_TABLE_  /* This contains the linked address of the GOT */                                                 
        /* The ra register now contains the runtime address of the above memory location */                                              

        .word   . - 4                  /* This contains the link time address of the previous word,                                      
                                        which is also what the link time expected PC value is */                                         
1:                                                                                                
        move    gp, ra    /* Move current PC into gp register */                                                                         
        lw      a5, 0(ra) /* Load linked address of the GOT into a5 */                                                                   
        lw      a6, 4(ra) /* Load the link time address of the GOT storage location into a6 */                                           
        sub     a5, a6    /* Subtract a6 from t1. */                                                                                     
        /* a5 now contains the difference between the link-time GOT table address and the link time expected PC */                       

        /* Add this difference to the current PC (copied into gp above) so that gp now has the current runtime                           
        ** GOT table address */                                                                                                          
        daddu   gp, a5  # calculate current location of offset table              

I don't understand 3 directive above:

.extern _GLOBAL_OFFSET_TABLE_
.word _GLOBAL_OFFSET_TABLE_ //what the meaning after declaring it as a external symbol?
.word . -4  //and also this one?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-10-27 02:45:45
.extern _GLOBAL_OFFSET_TABLE_

该符号在此编译单元之外进行描述。在链接时解决它。

.word _GLOBAL_OFFSET_TABLE_

分配一个字的内存并将符号的地址放置在该位置

.word . - 4

取当前位置.,减去4并将该值放置在内存的一个字中。与前面的代码类似,只是正在计算放置在该位置的值。

这是一个链接许多汇编指令的列表。

.extern _GLOBAL_OFFSET_TABLE_

The symbol is described outside of this compilation unit. Resolve it at link time.

.word _GLOBAL_OFFSET_TABLE_

Allocate a word worth of memory and place the address of the symbol at this location

.word . - 4

Take the current location, ., subtract 4 and place that value in a word of memory. Similar to the previous code except the value being placed at this location is being computed.

Here is a link with a list of many of the assembler directives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文