#include <stdio.h>
#include <libelf.h>
#include <gelf.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
static const char *symbind[STB_NUM] = {
"LOCL",
"GLOB",
"WEAK"
};
static const char *symtype[STT_NUM] = {
"NOTY",
"OBJT",
"FUNC",
"SECT",
"FILE",
"COMM",
"TLS"
};
#if STT_NUM != (STT_TLS + 1)
#error "STT_NUM has grown. Update symtype[]."
#endif
#define INTSTRLEN 32
static void
print_symtab(Elf *elf, const char *file)
{
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
GElf_Ehdr ehdr;
size_t shstrndx;
if (gelf_getehdr(elf, &ehdr) == NULL) {
(void) fprintf(stderr, "%s: elf_getehdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
(void) fprintf(stderr, "%s: elf_getshdrstrndx() failed: %s\n",
file, elf_errmsg(0));
return;
}
while ((scn = elf_nextscn(elf, scn)) != NULL) {
uint_t symcnt, ndx, nosymshndx;
Elf_Data *symdata, *shndxdata;
if (gelf_getshdr(scn, &shdr) == NULL) {
(void) fprintf(stderr,
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
if ((shdr.sh_type != SHT_SYMTAB) &&
(shdr.sh_type != SHT_DYNSYM) &&
(shdr.sh_type != SHT_SUNW_LDYNSYM))
continue;
if ((symdata = elf_getdata(scn, NULL)) == NULL) {
(void) fprintf(stderr,
"%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
(void) printf("\nSymTab: %s:%s\n", file,
elf_strptr(elf, shstrndx, shdr.sh_name));
(void) printf(" index value size type "
"bind oth shndx name\n");
shndxdata = 0;
nosymshndx = 0;
symcnt = shdr.sh_size / shdr.sh_entsize;
for (ndx = 0; ndx < symcnt; ndx++) {
GElf_Sym sym;
Elf32_Word shndx;
uint_t type, bind, specshndx;
char bindbuf[INTSTRLEN];
char typebuf[INTSTRLEN];
char shndxbuf[INTSTRLEN];
const char *bindstr, *typestr, *shndxstr;
if (gelf_getsymshndx(symdata, shndxdata, ndx,
&sym, &shndx) == NULL) {
(void) fprintf(stderr,
"%s: gelf_getsymshndx() failed: %s\n",
file, elf_errmsg(0));
return;
}
if ((sym.st_shndx == SHN_XINDEX) &&
(shndxdata == 0) && (nosymshndx == 0)) {
Elf_Scn *_scn = NULL;
GElf_Shdr _shdr;
GElf_Word symscnndx;
specshndx = 0;
symscnndx = elf_ndxscn(scn);
while ((_scn =
elf_nextscn(elf, _scn)) != NULL) {
if (gelf_getshdr(_scn, &_shdr) == NULL)
break;
if ((_shdr.sh_type ==
SHT_SYMTAB_SHNDX) &&
(_shdr.sh_link == symscnndx) &&
((shndxdata = elf_getdata(_scn,
NULL)) != NULL))
break;
}
if (shndxdata &&
(gelf_getsymshndx(symdata, shndxdata, ndx,
&sym, &shndx) == NULL)) {
(void) fprintf(stderr,
"%s: gelf_getsymshndx() "
"failed: %s\n",
file, elf_errmsg(0));
return;
}
if (shndxdata == 0)
nosymshndx = 1;
}
type = GELF_ST_TYPE(sym.st_info);
bind = GELF_ST_BIND(sym.st_info);
if (type < STT_NUM)
typestr = symtype[type];
else {
(void) snprintf(typebuf, INTSTRLEN,
"%d", type);
typestr = typebuf;
}
if (bind < STB_NUM)
bindstr = symbind[bind];
else {
(void) snprintf(bindbuf, INTSTRLEN,
"%d", bind);
bindstr = bindbuf;
}
specshndx = 0;
if (sym.st_shndx < SHN_LORESERVE)
shndx = sym.st_shndx;
else if ((sym.st_shndx != SHN_XINDEX) ||
(shndxdata == NULL)) {
shndx = sym.st_shndx;
specshndx = 1;
}
if (shndx == SHN_UNDEF) {
shndxstr = (const char *)"UNDEF";
} else if (specshndx) {
if (shndx == SHN_ABS)
shndxstr = (const char *)"ABS";
else if (shndx == SHN_COMMON)
shndxstr = (const char *)"COMM";
else if (shndx == SHN_XINDEX)
shndxstr = (const char *)"XIND";
else {
(void) snprintf(shndxbuf, INTSTRLEN,
"%ld", shndx);
shndxstr = shndxbuf;
}
} else {
(void) snprintf(shndxbuf, INTSTRLEN,
"%ld", shndx);
shndxstr = shndxbuf;
}
(void) printf("[%3d] 0x%08llx 0x%08llx %-4s "
"%-6s %2d %5s %s\n",
ndx, sym.st_value, sym.st_size,
typestr, bindstr, sym.st_other, shndxstr,
elf_strptr(elf, shdr.sh_link, sym.st_name));
}
}
}
static void
process_elf(Elf *elf, char *file, int fd, int member)
{
Elf_Cmd cmd;
Elf *_elf;
switch (elf_kind(elf)) {
case ELF_K_ELF:
print_symtab(elf, file);
break;
case ELF_K_AR:
cmd = ELF_C_READ;
while ((_elf = elf_begin(fd, cmd, elf)) != NULL) {
Elf_Arhdr *arhdr;
char buffer[1024];
arhdr = elf_getarhdr(_elf);
(void) snprintf(buffer, 1024, "%s(%s)",
file, arhdr->ar_name);
process_elf(_elf, buffer, fd, 1);
cmd = elf_next(_elf);
(void) elf_end(_elf);
}
break;
default:
if (!member)
(void) fprintf(stderr,
"%s: unexpected elf_kind(): 0x%x\n",
file, elf_kind(elf));
return;
}
}
int
main(int argc, char **argv)
{
int i;
if (argc < 2) {
(void) printf("usage: %s elf_file ...\n", argv[0]);
return (1);
}
if (elf_version(EV_CURRENT) == EV_NONE) {
(void) fprintf(stderr,
"elf_version() failed: %s\n", elf_errmsg(0));
return (1);
}
for (i = 1; i < argc; i++) {
int fd;
Elf *elf;
char *elf_fname;
elf_fname = argv[i];
if ((fd = open(elf_fname, O_RDONLY)) == -1) {
perror("open");
continue;
}
if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) {
(void) fprintf(stderr, "elf_begin() failed: %s\n",
elf_errmsg(0));
(void) close(fd);
continue;
}
process_elf(elf, elf_fname, fd, 0);
(void) elf_end(elf);
(void) close(fd);
}
return (0);
}