Add a couple of toy programs.

CVS patchset: 5508
CVS date: 2002/06/18 17:52:02
This commit is contained in:
jbj 2002-06-18 17:52:02 +00:00
parent 7de7470228
commit 448e44da6c
2 changed files with 88 additions and 0 deletions

46
libelf/lib/t1.c Executable file
View File

@ -0,0 +1,46 @@
#include <fcntl.h>
#include <stdio.h>
#include <libelf.h>
#include <stdlib.h>
#include <string.h>
static void
failure(void)
{
(void) fprintf(stderr, "%s\n", elf_errmsg(elf_errno()));
exit(1);
}
void
main(int argc, char ** argv)
{
Elf32_Shdr * shdr;
Elf32_Ehdr * ehdr;
Elf * elf;
Elf_Scn * scn;
Elf_Data * data;
int fd;
unsigned int cnt;
/* Open the input file */
if ((fd = open(argv[1], O_RDONLY)) == -1)
exit(1);
/* Obtain the ELF descriptor */
(void) elf_version(EV_CURRENT);
if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
failure();
/* Obtain the .shstrtab data buffer */
if (((ehdr = elf32_getehdr(elf)) == NULL) ||
((scn = elf_getscn(elf, ehdr->e_shstrndx)) == NULL) ||
((data = elf_getdata(scn, NULL)) == NULL))
failure();
/* Traverse input filename, printing each section */
for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) {
if ((shdr = elf32_getshdr(scn)) == NULL)
failure();
(void) printf("[%d] %s\n", cnt, (char *)data->d_buf + shdr->sh_name);
}
} /* end main */

42
libelf/lib/t2.c Executable file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libelf.h>
#include <gelf.h>
int
main(int argc, char **argv)
{
Elf *elf;
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
Elf_Data *data;
int fd, ii, count;
elf_version(EV_CURRENT);
fd = open(argv[1], O_RDONLY);
elf = elf_begin(fd, ELF_C_READ, NULL);
while ((scn = elf_nextscn(elf, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_SYMTAB) {
/* found a symbol table, go print it. */
break;
}
}
data = elf_getdata(scn, NULL);
count = shdr.sh_size / shdr.sh_entsize;
/* print the symbol names */
for (ii = 0; ii < count; ++ii) {
GElf_Sym sym;
gelf_getsym(data, ii, &sym);
printf("%s\n", elf_strptr(elf, shdr.sh_link, sym.st_name));
}
elf_end(elf);
close(fd);
}