Jér*_*ier 7 c unix command elf nm
我正在研究ELF格式.我必须编写一个简单的nm函数(没有选项).我已经在输出上打印了符号的值和符号的名称.
这是nm输出:
value type name
0000000000600e30 D __DTOR_END__
Run Code Online (Sandbox Code Playgroud)
我有相同的,但没有'类型'.我正在使用ELF64_Sym结构,如下所示:
typedef struct {
Elf64_Word st_name;
unsigned char st_info;
unsigned char st_other;
Elf64_Half st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
Run Code Online (Sandbox Code Playgroud)
我知道我必须使用st_info变量和这个宏:
#define ELF64_ST_TYPE(info) ((info) & 0xf)
Run Code Online (Sandbox Code Playgroud)
获取符号的类型.但是,符号类型可以是一个宏,如下所示:
NAME VALUE
STT_NOTYPE 0
STT_OBJECT 1
STT_FUNC 2
STT_SECTION 3
STT_FILE 4
STT_LOPROC 13
STT_HIOPROC 15
Run Code Online (Sandbox Code Playgroud)
我想知道如何从这些宏中获取由nm打印的字母,例如:
U, u, A, a, T, t, R, r, W, w
Run Code Online (Sandbox Code Playgroud)
好的,我做了一些研究,这是我的功能,根据符号得到正确的字符.随意添加/编辑一些字符.
char print_type(Elf64_Sym sym, Elf64_Shdr *shdr)
{
char c;
if (ELF64_ST_BIND(sym.st_info) == STB_GNU_UNIQUE)
c = 'u';
else if (ELF64_ST_BIND(sym.st_info) == STB_WEAK)
{
c = 'W';
if (sym.st_shndx == SHN_UNDEF)
c = 'w';
}
else if (ELF64_ST_BIND(sym.st_info) == STB_WEAK && ELF64_ST_TYPE(sym.st_info) == STT_OBJECT)
{
c = 'V';
if (sym.st_shndx == SHN_UNDEF)
c = 'v';
}
else if (sym.st_shndx == SHN_UNDEF)
c = 'U';
else if (sym.st_shndx == SHN_ABS)
c = 'A';
else if (sym.st_shndx == SHN_COMMON)
c = 'C';
else if (shdr[sym.st_shndx].sh_type == SHT_NOBITS
&& shdr[sym.st_shndx].sh_flags == (SHF_ALLOC | SHF_WRITE))
c = 'B';
else if (shdr[sym.st_shndx].sh_type == SHT_PROGBITS
&& shdr[sym.st_shndx].sh_flags == SHF_ALLOC)
c = 'R';
else if (shdr[sym.st_shndx].sh_type == SHT_PROGBITS
&& shdr[sym.st_shndx].sh_flags == (SHF_ALLOC | SHF_WRITE))
c = 'D';
else if (shdr[sym.st_shndx].sh_type == SHT_PROGBITS
&& shdr[sym.st_shndx].sh_flags == (SHF_ALLOC | SHF_EXECINSTR))
c = 'T';
else if (shdr[sym.st_shndx].sh_type == SHT_DYNAMIC)
c = 'D';
else
c = '?';
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL && c != '?')
c += 32;
return c;
}
Run Code Online (Sandbox Code Playgroud)
我错过了s,n,p和i.我很确定'R'不好.当我找到它时,我会编辑它.