如何获得ELF文件的值?

时间:2021-05-07 04:16:54

I'm Beginner of Linux System and I'm studying ELF File Format reading this Documents(http://www.skyfree.org/linux/references/ELF_Format.pdf). But When i see related Document about Relocation, There is Strange Things in Relocation Calculation. i know according to relocation type, it applies different way to calculate. But look at this.

我是Linux系统的初学者,我正在学习阅读这些文档的ELF文件格式(http://www.skyfree.org/linux/references/ELF_Format.pdf)。但是当我看到有关搬迁的相关文件时,在搬迁计算中有一些奇怪的事情。我知道根据搬迁类型,它适用不同的计算方式。但是看看这个。

When R-type is R_386_RELATIVE, this document says the way to calculate "B + A". However What is "A" meaning exactly? and How can I get this "A" Value in ELF File?

当r类型为R_386_RELATIVE时,本文给出计算“B + A”的方法。然而“A”到底是什么意思呢?如何在ELF文件中获得这个“A”值?

Please give me ur merciful answer.......;

请给我一份仁慈的答复。

1 个解决方案

#1


2  

From document you mentioned:

你提到的文件:

R_386_RELATIVE

R_386_RELATIVE

The link editor creates this relocation type for dynamic linking. Its offset member gives a location within a shared object that contains a value representing a relative address. The dynamic linker computes the corresponding virtual address by adding the virtual address at which the shared object was loaded to the relative address. Relocation entries for this type must specify 0 for the symbol table index.

链接编辑器为动态链接创建这种重新定位类型。它的偏移量成员在一个共享对象中给出一个位置,该对象包含一个表示相对地址的值。动态链接器通过将共享对象加载到相对地址的虚拟地址来计算相应的虚拟地址。这种类型的重定位条目必须为符号表索引指定0。

A

一个

This means the addend used to compute the value of the relocatable field.

这意味着用于计算可重定位字段值的加数。

B

B

This means the base address at which a shared object has been loaded into memory during execution. Generally, a shared object file is built with a 0 base virtual address, but the execution address will be different.

这意味着共享对象在执行期间被加载到内存中的基本地址。通常,一个共享对象文件是用一个0基的虚拟地址构建的,但是执行地址是不同的。

Addend

加数

As shown above, only Elf32_Rela entries contain an explicit addend. Entries of type `Elf32_Rel store an implicit addend in the location to be modified. Depending on the processor architecture, one form or the other might be necessary or more convenient. Consequently, an implementation for a particular machine may use one form exclusively or either form depending on context.

如上所示,只有Elf32_Rela条目包含显式的加数。类型“Elf32_Rel”存储在要修改的位置的隐式addend。根据处理器体系结构的不同,一种形式或另一种形式可能是必要的或更方便的。因此,特定机器的实现可能只使用一种形式,也可以根据上下文使用某种形式。

Base Address

基地址

To compute the base address, one determines the memory address associated with the lowest p_vaddr value for a PT_LOAD segment. One then obtains the base address by truncating the memory address to the nearest multiple of the maximum page size. Depending on the kind of file being loaded into memory, the memory address might or might not match the p_vaddr values.

要计算基地址,可以确定与PT_LOAD段的最低p_vaddr值相关联的内存地址。然后,通过将内存地址截断到最大页面大小的最近倍数来获取基本地址。根据加载到内存中的文件的类型,内存地址可能与p_vaddr值匹配,也可能与之不匹配。

So it boils down to next:

所以它可以归结为:

  • A is addend and calculated from Elf32_Rel or Elf32_Rela structure
  • A是addend,由Elf32_Rel或Elf32_Rela结构计算。
  • B is base address and calculated from p_vaddr. Particular calculation depends on architecture.
  • B是基地址,由p_vaddr计算。具体的计算取决于体系结构。

You can observe relocation section of some binary/library file using readelf -r.

您可以使用readelf -r观察一些二进制/库文件的重定位部分。

#1


2  

From document you mentioned:

你提到的文件:

R_386_RELATIVE

R_386_RELATIVE

The link editor creates this relocation type for dynamic linking. Its offset member gives a location within a shared object that contains a value representing a relative address. The dynamic linker computes the corresponding virtual address by adding the virtual address at which the shared object was loaded to the relative address. Relocation entries for this type must specify 0 for the symbol table index.

链接编辑器为动态链接创建这种重新定位类型。它的偏移量成员在一个共享对象中给出一个位置,该对象包含一个表示相对地址的值。动态链接器通过将共享对象加载到相对地址的虚拟地址来计算相应的虚拟地址。这种类型的重定位条目必须为符号表索引指定0。

A

一个

This means the addend used to compute the value of the relocatable field.

这意味着用于计算可重定位字段值的加数。

B

B

This means the base address at which a shared object has been loaded into memory during execution. Generally, a shared object file is built with a 0 base virtual address, but the execution address will be different.

这意味着共享对象在执行期间被加载到内存中的基本地址。通常,一个共享对象文件是用一个0基的虚拟地址构建的,但是执行地址是不同的。

Addend

加数

As shown above, only Elf32_Rela entries contain an explicit addend. Entries of type `Elf32_Rel store an implicit addend in the location to be modified. Depending on the processor architecture, one form or the other might be necessary or more convenient. Consequently, an implementation for a particular machine may use one form exclusively or either form depending on context.

如上所示,只有Elf32_Rela条目包含显式的加数。类型“Elf32_Rel”存储在要修改的位置的隐式addend。根据处理器体系结构的不同,一种形式或另一种形式可能是必要的或更方便的。因此,特定机器的实现可能只使用一种形式,也可以根据上下文使用某种形式。

Base Address

基地址

To compute the base address, one determines the memory address associated with the lowest p_vaddr value for a PT_LOAD segment. One then obtains the base address by truncating the memory address to the nearest multiple of the maximum page size. Depending on the kind of file being loaded into memory, the memory address might or might not match the p_vaddr values.

要计算基地址,可以确定与PT_LOAD段的最低p_vaddr值相关联的内存地址。然后,通过将内存地址截断到最大页面大小的最近倍数来获取基本地址。根据加载到内存中的文件的类型,内存地址可能与p_vaddr值匹配,也可能与之不匹配。

So it boils down to next:

所以它可以归结为:

  • A is addend and calculated from Elf32_Rel or Elf32_Rela structure
  • A是addend,由Elf32_Rel或Elf32_Rela结构计算。
  • B is base address and calculated from p_vaddr. Particular calculation depends on architecture.
  • B是基地址,由p_vaddr计算。具体的计算取决于体系结构。

You can observe relocation section of some binary/library file using readelf -r.

您可以使用readelf -r观察一些二进制/库文件的重定位部分。