7 #if defined(HAVE_SYS_LINK_H)
8 # include <sys/link.h> /* Find DT_RPATH on Solaris 2.6 */
9 #endif /* HAVE_SYS_LINK_H */
13 #include <sys/types.h>
19 #define EHDR_PWS(x) (is_e32() ? DO_SWAPS32(ehdr->e32.x) : DO_SWAPS64(ehdr->e64.x))
20 #define EHDR_PHS(x) (is_e32() ? DO_SWAPS16(ehdr->e32.x) : DO_SWAPS16(ehdr->e64.x))
21 #define PHDR_PWS(x) (is_e32() ? DO_SWAPS32(phdr->e32.x) : DO_SWAPS64(phdr->e64.x))
22 #define EHDR_PWU(x) (is_e32() ? DO_SWAPU32(ehdr->e32.x) : DO_SWAPU64(ehdr->e64.x))
23 #define EHDR_PHU(x) (is_e32() ? DO_SWAPU16(ehdr->e32.x) : DO_SWAPU16(ehdr->e64.x))
24 #define PHDR_PWU(x) (is_e32() ? DO_SWAPU32(phdr->e32.x) : DO_SWAPU32(phdr->e64.x))
25 #define PHDR_POU(x) (is_e32() ? DO_SWAPU32(phdr->e32.x) : DO_SWAPU64(phdr->e64.x))
27 static int is_e32_flag;
28 static int swap_bytes_flag;
39 return swap_bytes_flag;
43 elf_open(const char *filename, int flags, Elf_Ehdr *ehdr)
49 fd = open(filename, flags);
56 if (read(fd, ehdr, EI_NIDENT) != EI_NIDENT)
58 perror ("reading header (e_ident)");
63 if (0 != memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
64 (ehdr->e_ident[EI_CLASS] != ELFCLASS32 &&
65 ehdr->e_ident[EI_CLASS] != ELFCLASS64) ||
66 (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
67 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) ||
68 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
70 fprintf(stderr, "`%s' probably isn't an ELF file.\n", filename);
72 errno = ENOEXEC; /* Hm, is this the best errno code to use? */
76 is_e32_flag = ehdr->e_ident[EI_CLASS] == ELFCLASS32;
77 swap_bytes_flag = ehdr->e_ident[EI_DATA] != ELFDATA2;
79 sz_ehdr = is_e32() ? sizeof(Elf32_Ehdr) : sizeof(Elf64_Ehdr);
80 if (read(fd, ((char *)ehdr) + EI_NIDENT, sz_ehdr - EI_NIDENT)
81 != (ssize_t)(sz_ehdr - EI_NIDENT))
83 perror ("reading header");
88 sz_phdr = is_e32() ? sizeof(Elf32_Phdr) : sizeof(Elf64_Phdr);
89 if ((size_t)EHDR_PHS(e_phentsize) != sz_phdr)
91 fprintf(stderr, "section size was read as %zd, not %zd!\n",
92 (size_t)EHDR_PHS(e_phentsize), sz_phdr);
100 elf_find_dynamic_section(int fd, Elf_Ehdr *ehdr, Elf_Phdr *phdr)
103 if (lseek(fd, EHDR_PWU(e_phoff), SEEK_SET) == -1)
105 perror ("positioning for sections");
109 for (i = 0; i < EHDR_PHS(e_phnum); i++)
111 const size_t sz_phdr = is_e32() ? sizeof(Elf32_Phdr) : sizeof(Elf64_Phdr);
112 if (read(fd, phdr, sz_phdr) != (ssize_t)sz_phdr)
114 perror ("reading section header");
117 if (PHDR_PWU(p_type) == PT_DYNAMIC)
120 if (i == EHDR_PHS(e_phnum))
122 fprintf (stderr, "No dynamic section found.\n");
126 if (0 == PHDR_POU(p_filesz))
128 fprintf (stderr, "Length of dynamic section is zero.\n");
148 #if defined(DT_RUNPATH)
152 #endif /* DT_RUNPATH */
158 elf_dynpath_tag(int tag)
160 return ( tag == DT_RPATH
161 #if defined(DT_RUNPATH)
163 #endif /* DT_RUNPATH */