微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C 如何将条目的 st_value 转换为值?

如何解决C 如何将条目的 st_value 转换为值?

当我在我的 可执行 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))

解决方法

00000000004004e74195559 的十六进制表示,也就是您的 st_value

您可以使用 %xprintf() 以十六进制打印值。添加类似 %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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。