在目标文件符号表和重定位表 [英] symbol table and relocation table in object file

查看:1492
本文介绍了在目标文件符号表和重定位表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,指令和数据在目标文件中都有地址。第一个数据项的起始地址是0和第一条指令开始也会在地址0

重定位表包含有关需要更新,如果在该文件改变的地址,例如,如果文件与另一个连接在一起的指令的信息。 A线,在下面的例子中,将在重定位表。我不认为B.将在重定位表,因为标记的平等的地址是相对于B.这些是正确的假设?

我认识的符号表显示文件有标签,标签也还没有得到解决。但符号表包含其他什么信息?

另外,当汇编转换为二进制的指示,什么被放置在那些有未解决的引用的指令?中的B这个例子。

 。数据
TEXT:.asciiz富。文本
。全球主要
主要:
     李T0,1
     BEQ T0,1,等于#B等于:
    LA A0,TEXT
    日航的printf #A


解决方案

是的,你的假设是正确的。有各种类型迁移的,什么汇编发射到指令取决于类型。通常它是一个偏移量被添加。您可以使用 objdump的-dr 看到重定位。为了更好的说明我已经改变了code一点:

 。数据
.INT 0
TEXT:.asciiz富
。文本
。全球主要
主要:
     李$ T0,1
     BEQ $ T0,1,等于#B
     BNE $ T0,42,富#C等于:
     LA $ A0,TEXT
     日航的printf #A

objdump的输出:

  00000000<主计算值:
   0:24080001李T0,1
   4:24010001李为,1
   8:11010004 BEQ T0,在,1C<&等于GT;
   C:00000000 NOP
  10:2401002a李在42
  14:1501ffff BNE t0时,在14<主+ 0x14的>
                        14:R_MIPS_PC16富
  18:00000000 NOP0000001c<相等的计算值:
  1C:3c040000吕a0,0x0
                        1C:R_MIPS_HI16。数据
  20:0c000000 JAL 0℃;主>
                        20:R_MIPS_26的printf
  24:24840004 ADDIU A0,a0,4
                        24:R_MIPS_LO16。数据

正如你所说,没有搬迁的 BEQ 因为这是这个目标文件。

内的相对地址

BNE 我加入(标记 C线)引用外部符号,因此,即使该地址是需要相对重定位记录。这将是类型 R_MIPS_PC16 ,生产出16位有符号的字偏移符号。由于指令编码需要从下一个字偏移量,而不是当前的 PC 的搬迁使用, 1 必须是扣除,这就是恩codeD为2的补 FFFF 到指令本身。

pseudoinstruction已被翻译由汇编成 / ADDIU 对(后者在延迟槽的 JAL )。对于 A R_MIPS_HI16 搬迁是对。数据部分将在高16位补。由于符号 TEXT 是在。数据 4 >部分,偏移的前16位是 0 。这意味着指令包含 0 偏移。同样,对于低16位,但有指包含 4 的偏移。

最后, JAL的printf 使用另一种类型的搬迁是由指令所需的编码量身定做。偏移量是零,因为跳是直接向所引用的符号。需要注意的是objdump的尝试是通过解码是有益的,但​​它不处理搬迁,让<主方式> 它输出当然是无稽之谈

From what I understand, instructions and data in an object file all have addresses. First data item start at address 0 and first instruction also start at address 0.

The relocation table contains information about instructions that need to be updated if the addresses in the file change, for example if the file is linked together with another. Line A, in the example below, would be in the relocation table. I don't think B would be in the relocation table, since the address of label "equal" is relative to B. Are these correct assumptions?

I know the symbol table show the labels the file have and also labels that haven't been resolved. But what other information does the symbol table contain?

Also, when the assembler translates the instructions to binary, what is placed in those instructions that have unresolved references?. B in this example.

.data
TEXT: .asciiz "Foo"

.text
.global main
main:
     li t0, 1
     beq t0, 1, equal #B

equal:
    la a0, TEXT
    jal printf #A

解决方案

Yes, your assumptions are correct. There are various types of relocations, what the assembler emits into the instruction depends on the type. Generally it's an offset to be added. You can use objdump -dr to see relocations. For better illustration I have changed your code a little:

.data
.int 0
TEXT: .asciiz "Foo"
.text
.global main
main:
     li $t0, 1
     beq $t0, 1, equal #B
     bne $t0, 42, foo  #C

equal:
     la $a0, TEXT
     jal printf #A

Output of objdump:

00000000 <main>:
   0:   24080001        li      t0,1
   4:   24010001        li      at,1
   8:   11010004        beq     t0,at,1c <equal>
   c:   00000000        nop
  10:   2401002a        li      at,42
  14:   1501ffff        bne     t0,at,14 <main+0x14>
                        14: R_MIPS_PC16 foo
  18:   00000000        nop

0000001c <equal>:
  1c:   3c040000        lui     a0,0x0
                        1c: R_MIPS_HI16 .data
  20:   0c000000        jal     0 <main>
                        20: R_MIPS_26   printf
  24:   24840004        addiu   a0,a0,4
                        24: R_MIPS_LO16 .data

As you said, there is no relocation for the beq since that's a relative address within this object file.

The bne I added (line marked with C) references an external symbol, so even though the address is relative a relocation entry is needed. It will be of type R_MIPS_PC16 to produce a 16 bit signed word offset to symbol foo. As the instruction encoding requires offset from the next word and not the current PC that the relocation uses, 1 has to be subtracted, and that's encoded as 2's complement ffff into the instruction itself.

The la pseudoinstruction has been translated by the assembler into a lui/addiu pair (the latter in the delay slot of the jal). For the lui a R_MIPS_HI16 relocation is created against the .data section which will fill in the top 16 bits. Since the symbol TEXT is at address 4 in the .data section, the top 16 bits of the offset are 0. This means the instruction contains 0 offset. Similarly, for the low 16 bits, except there the instruction contains an offset of 4.

Finally, the jal printf is using yet another kind of relocation that is tailored for the encoding required by the instruction. The offset is zero because the jump is directly to the referenced symbol. Note that objdump is trying to be helpful by decoding that, but it doesn't process the relocation so the <main> it outputs is of course nonsense.

这篇关于在目标文件符号表和重定位表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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