diff options
author | jbj <devnull@localhost> | 2002-06-18 17:52:02 +0000 |
---|---|---|
committer | jbj <devnull@localhost> | 2002-06-18 17:52:02 +0000 |
commit | 448e44da6c79f1eb6789d6922e05a4b15e66c96a (patch) | |
tree | 37e52bdadcd22f2c4a62c298c4e7b035327eab90 /libelf | |
parent | 7de7470228c6d8adc42872554b5de7b8dee3f261 (diff) | |
download | librpm-tizen-448e44da6c79f1eb6789d6922e05a4b15e66c96a.tar.gz librpm-tizen-448e44da6c79f1eb6789d6922e05a4b15e66c96a.tar.bz2 librpm-tizen-448e44da6c79f1eb6789d6922e05a4b15e66c96a.zip |
Add a couple of toy programs.
CVS patchset: 5508
CVS date: 2002/06/18 17:52:02
Diffstat (limited to 'libelf')
-rwxr-xr-x | libelf/lib/t1.c | 46 | ||||
-rwxr-xr-x | libelf/lib/t2.c | 42 |
2 files changed, 88 insertions, 0 deletions
diff --git a/libelf/lib/t1.c b/libelf/lib/t1.c new file mode 100755 index 000000000..ded243375 --- /dev/null +++ b/libelf/lib/t1.c @@ -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 */ diff --git a/libelf/lib/t2.c b/libelf/lib/t2.c new file mode 100755 index 000000000..9449f253b --- /dev/null +++ b/libelf/lib/t2.c @@ -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); +} |