/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* dispsyms: Display Symbols
*
* This program demonstrates the use of the libelf interface to
* read an ELF file. dispsyms will open an ELF file using
* elf_begin(ELF_C_READ) and examine search the ELF file
* for a symbol table (SHT_SYMTAB, SHT_DYNSYM, or SHT_SUNW_LDYNSYM).
* It will display the contents of any symbol tables it finds.
*
* Note: This program also understands about the use
* of 'Extended ELF Section indexes' and will
* decode a corresponding SHT_SYMTAB_SHNDX
* section if required.
*/
#include <stdio.h>
#include <libelf.h>
#include <gelf.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/* STB_LOCL */ "LOCL",
/* STB_GLOBAL */ "GLOB",
/* STB_WEAK */ "WEAK"
};
/* STT_NOTYPE */ "NOTY",
/* STT_OBJECT */ "OBJT",
/* STT_FUNC */ "FUNC",
/* STT_SECTION */ "SECT",
/* STT_FILE */ "FILE",
/* STT_COMMON */ "COMM",
/* STT_TLS */ "TLS"
};
#error "STT_NUM has grown. Update symtype[]."
#endif
static void
{
file, elf_errmsg(0));
return;
}
file, elf_errmsg(0));
return;
}
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
continue;
/*
* Get the data associated with the Symbol
* section.
*/
"%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
/*
* Print symbol table title and header for symbol display
*/
(void) printf(" index value size type "
"bind oth shndx name\n");
/*
* We now iterate over the full symbol table printing
* the symbols as we go.
*/
shndxdata = 0;
nosymshndx = 0;
/*
* Get a symbol entry
*/
"%s: gelf_getsymshndx() failed: %s\n",
file, elf_errmsg(0));
return;
}
/*
* Check to see if this symbol's st_shndx is using
* the 'Extended SHNDX table' for a SYMTAB.
*
* If it is - and we haven't searched before, go
* find the associated SHT_SYMTAB_SHNDX section.
*/
(shndxdata == 0) && (nosymshndx == 0)) {
specshndx = 0;
while ((_scn =
break;
/*
* We've found the Symtab SHNDX table
* if it's of type SHT_SYMTAB_SHNDX
* and it's shdr.sh_link points to the
* section index for the current symbol
* table.
*/
SHT_SYMTAB_SHNDX) &&
break;
}
/*
* Get a symbol entry
*/
if (shndxdata &&
"%s: gelf_getsymshndx() "
"failed: %s\n",
file, elf_errmsg(0));
return;
}
/*
* No Symtab SHNDX table was found. We could
* give a fatal error here - instead we'll
* just mark that fact and display as much of
* the symbol table as we can. Any symbol
* displayed with a XINDX section index has
* a bogus value.
*/
if (shndxdata == 0)
nosymshndx = 1;
}
/*
* Decode the type & binding information
*/
else {
"%d", type);
}
else {
"%d", bind);
}
specshndx = 0;
specshndx = 1;
}
shndxstr = (const char *)"UNDEF";
} else if (specshndx) {
shndxstr = (const char *)"ABS";
else if (shndx == SHN_COMMON)
shndxstr = (const char *)"COMM";
else if (shndx == SHN_XINDEX)
shndxstr = (const char *)"XIND";
else {
"%ld", shndx);
}
} else {
"%ld", shndx);
}
/*
* Display the symbol entry.
*/
(void) printf("[%3d] 0x%08llx 0x%08llx %-4s "
"%-6s %2d %5s %s\n",
}
}
}
static void
{
case ELF_K_ELF:
/*
* This is an ELF file, now attempt to find it's
* .comment section and to display it.
*/
break;
case ELF_K_AR:
/*
* Archives contain multiple ELF files, which can each
* in turn be examined with libelf.
*
* The below loop will iterate over each member of the
* archive and recursively call process_elf().
*/
cmd = ELF_C_READ;
/*
* Build up file names based off of
* 'archivename(membername)'.
*/
/*
* Recursively process the ELF members.
*/
}
break;
default:
if (!member)
"%s: unexpected elf_kind(): 0x%x\n",
return;
}
}
int
{
int i;
if (argc < 2) {
return (1);
}
/*
* Initialize the elf library, must be called before elf_begin()
* can be called.
*/
"elf_version() failed: %s\n", elf_errmsg(0));
return (1);
}
for (i = 1; i < argc; i++) {
int fd;
char *elf_fname;
perror("open");
continue;
}
/*
* Attempt to open an Elf descriptor Read-Only
* for each file.
*/
elf_errmsg(0));
continue;
}
/*
* Process each elf descriptor.
*/
}
return (0);
}