如何解释 elf 符号表部分的 st_info 字段

4

这是手册页面的内容:

st_info   This  member  specifies  the  symbol's   type   and   binding
             attributes:

             STT_NOTYPE  The symbol's type is not defined.

             STT_OBJECT  The symbol is associated with a data object.

             STT_FUNC    The symbol is associated with a function or other
                         executable code.

             STT_SECTION The symbol is associated with a section.   Symbol
                         table  entries  of  this type exist primarily for
                         relocation and normally have STB_LOCAL bindings.

             STT_FILE    By convention, the symbol's name gives  the  name
                         of  the  source  file  associated with the object
                         file.  A file symbol has STB_LOCAL bindings,  its
                         section  index  is  SHN_ABS,  and it precedes the
                         other STB_LOCAL symbols of the  file,  if  it  is
                         present.

             STT_LOPROC  This  value  up  to  and  including STT_HIPROC is
                         reserved for processor-specific semantics.

             STT_HIPROC  This value down to and  including  STT_LOPROC  is
                         reserved for processor-specific semantics.

             STB_LOCAL   Local  symbols are not visible outside the object
                         file containing their definition.  Local  symbols
                         of  the  same  name  may  exist in multiple files
                         without interfering with each other.

             STB_GLOBAL  Global symbols are visible to  all  object  files
                         being  combined.   One  file's  definition  of  a
                         global  symbol  will   satisfy   another   file's
                         undefined reference to the same symbol.

             STB_WEAK    Weak  symbols  resemble global symbols, but their
                         definitions have lower precedence.

             STB_LOPROC  This value up  to  and  including  STB_HIPROC  is
                         reserved for processor-specific semantics.

             STB_HIPROC  This  value  down  to and including STB_LOPROC is
                         reserved for processor-specific semantics.

                         There are macros for packing  and  unpacking  the
                         binding and type fields:

                         ELF32_ST_BIND(info)     or    ELF64_ST_BIND(info)
                         extract a binding from an st_info value.

                         ELF32_ST_TYPE(info) or ELF64_ST_TYPE(info)
                         extract a type from an st_info value.

                         ELF32_ST_INFO(bind, type) or  ELF64_ST_INFO(bind,
                         type)
                         convert  a  binding  and  a  type into an st_info
                         value.

问题在于 STT_*STB_* 的值重叠了。这是在uapi/linux/elf.h中声明这些值的方式:
#define STB_LOCAL  0
#define STB_GLOBAL 1
#define STB_WEAK   2

#define STT_NOTYPE  0
#define STT_OBJECT  1
#define STT_FUNC    2
#define STT_SECTION 3
#define STT_FILE    4
#define STT_COMMON  5
#define STT_TLS     6

我认为我理解了STB_*值是'绑定',STT+*是'类型',但由于这些值重叠,在给定符号表的情况下,我如何确定如何解释此字段?

1个回答

6

我该如何确定如何解释这个字段?

使用ELF{32,64}_ST_BINDELF{32,64}_ST_TYPE宏将st_info分解成其组件。

查看/usr/include/elf.h。你会发现类似以下内容:

/* How to extract and insert information held in the st_info field.  */
#define ELF32_ST_BIND(val) ...
#define ELF32_ST_TYPE(val) ...

... etc.

1
谢谢你的回答,但我还是有些困惑。假设st_info的值为2,这是指STB_WEAK还是STT_FUNC - Stephen
哦,我明白了。#define ELF_ST_BIND(x) ((x) >> 4) #define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf) 这意味着如果 st_info == 34 那么它就是 STB_WEAK STT_FUNC,因为 34 >> 4 == 2 并且 34 & 0xff == 2。对吗? - Stephen
@Stephen 正确(但你不应该以“34”的术语来考虑它,只需将readelf -Ws的输出与STB_WEAK == ELF_ST_BIND(sym->st_info)进行比较即可)。 - Employed Russian
明白了。我正在用Python编写一个ELF解析器,而这个值让我感到困惑。 - Stephen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接