1 #define _POSIX_C_SOURCE 200112L
14 int main(int ac, char **av) {
20 Elf64_Shdr *dynsect = 0;
21 Elf64_Shdr *strtab = 0, *dyntab = 0;
25 if (lstat(av[1], &sbuf) == -1) {
28 if (!S_ISREG(sbuf.st_mode)) {
32 elffd = open(av[1], O_RDONLY);
36 if (fstat(elffd, &sbuf) == -1) {
39 /* not a regular file? */
40 if (!S_ISREG(sbuf.st_mode)) {
43 /* not at least the size of the elf header? */
44 if (sbuf.st_size < sizeof(Elf64_Ehdr)) {
48 elfbase = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, elffd, 0);
54 if (hdr->e_ident[EI_MAG0] != ELFMAG0
55 || hdr->e_ident[EI_MAG1] != ELFMAG1
56 || hdr->e_ident[EI_MAG2] != ELFMAG2
57 || hdr->e_ident[EI_MAG3] != ELFMAG3
62 /* only dynamic files will have an soname */
63 if (hdr->e_type != ET_DYN) {
67 switch (hdr->e_ident[EI_CLASS]) {
76 /* check endian ness */
77 switch (hdr->e_ident[EI_DATA]) {
78 case ELFDATA2LSB: break;
79 case ELFDATA2MSB: break;
84 /* find the section header table */
85 for (i = 0; i < hdr->e_shnum; i++) {
86 shdr = (Elf64_Shdr *)((char *)elfbase + hdr->e_shoff + i * hdr->e_shentsize);
87 if (shdr->sh_type == SHT_DYNAMIC) {
89 } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
100 name = (char *) elfbase + strtab->sh_offset;
101 for (i = 0; i < hdr->e_shnum; i++) {
102 shdr = (Elf64_Shdr *)((char *)elfbase + hdr->e_shoff + i * hdr->e_shentsize);
103 if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
111 dynname = (char *) elfbase + dyntab->sh_offset;
112 for (dent = (Elf64_Dyn *)((char *)elfbase + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
113 if (dent->d_tag == DT_SONAME) {
114 printf("%s\n", dynname + dent->d_un.d_val);