问题描述
当我在我的 可执行 Elf 文件上使用 -s 标志调用 readelf 时,我得到:
Num: Value Size Type Bind Vis Ndx Name
43: 00000000004004e7 30 FUNC GLOBAL DEFAULT 13 find_me_func
但是当我在索引 43 处读取符号表条目时,我有:
st_name: 259
st_info: 18 '\022'
st_other: 0 '\0000'
st_shndx: 13
st_value: 4195559
st_size: 30
我的问题是如何使用我必须获得的信息:00000000004004e7
?
我认为这与 st_value
注意:也许这些宏有帮助?
/*
* Dynamic structure. The ".dynamic" section contains an array of them.
*/
typedef struct {
Elf64_Sxword d_tag; /* Entry type. */
union {
Elf64_Xword d_val; /* Integer value. */
Elf64_Addr d_ptr; /* Address value. */
} d_un;
} Elf64_Dyn;
/*
* Relocation entries.
*/
/* Relocations that don't need an addend field. */
typedef struct {
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
} Elf64_Rel;
/* Relocations that need an addend field. */
typedef struct {
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
Elf64_Sxword r_addend; /* Addend. */
} Elf64_Rela;
/* Macros for accessing the fields of r_info. */
#define ELF64_R_SYM(info) ((info) >> 32)
#define ELF64_R_TYPE(info) ((info) & 0xffffffffL)
/* Macro for constructing r_info from field values. */
#define ELF64_R_INFO(sym,type) (((sym) << 32) + ((type) & 0xffffffffL))
#define ELF64_R_TYPE_DATA(info) (((Elf64_Xword)(info)<<32)>>40)
#define ELF64_R_TYPE_ID(info) (((Elf64_Xword)(info)<<56)>>56)
#define ELF64_R_TYPE_INFO(data,type) \
(((Elf64_Xword)(data)<<8)+(Elf64_Xword)(type))
解决方法
00000000004004e7
是 4195559
的十六进制表示,也就是您的 st_value
。
您可以使用 %x
和 printf()
以十六进制打印值。添加类似 %016x
的数字以指定位数。
#include <stdio.h>
int main(void) {
int st_value = 4195559;
printf("%016x\n",st_value);
return 0;
}
或者如果你想要 64 位值:
#include <stdio.h>
#include <inttypes.h>
int main(void) {
uint64_t st_value = UINT64_C(4195559);
printf("%016" PRIx64 "\n",st_value);
return 0;
}