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_PS(x) (is_e32() ? DO_SWAPS32(ehdr->e32.x) : DO_SWAPS64(ehdr->e64.x))
20 #define PHDR_PS(x) (is_e32() ? DO_SWAPS32(phdr->e32.x) : DO_SWAPS64(phdr->e64.x))
21 #define EHDR_PU(x) (is_e32() ? DO_SWAPU32(ehdr->e32.x) : DO_SWAPU64(ehdr->e64.x))
22 #define PHDR_PU(x) (is_e32() ? DO_SWAPU32(phdr->e32.x) : DO_SWAPU64(phdr->e64.x))
24 static int is_e32_flag;
25 static int swap_bytes_flag;
36 return swap_bytes_flag;
40 elf_open(const char *filename, int flags, Elf_Ehdr *ehdr)
46 fd = open(filename, flags);
53 if (read(fd, ehdr, EI_NIDENT) != EI_NIDENT)
55 perror ("reading header (e_ident)");
60 if (0 != memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
61 (ehdr->e_ident[EI_CLASS] != ELFCLASS32 &&
62 ehdr->e_ident[EI_CLASS] != ELFCLASS64) ||
63 (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
64 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) ||
65 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
67 fprintf(stderr, "`%s' probably isn't an ELF file.\n", filename);
69 errno = ENOEXEC; /* Hm, is this the best errno code to use? */
73 is_e32_flag = ehdr->e_ident[EI_CLASS] == ELFCLASS32;
74 swap_bytes_flag = ehdr->e_ident[EI_DATA] != ELFDATA2;
76 sz_ehdr = is_e32() ? sizeof(Elf32_Ehdr) : sizeof(Elf64_Ehdr);
77 if (read(fd, ((char *)ehdr) + EI_NIDENT, sz_ehdr - EI_NIDENT)
78 != (ssize_t)(sz_ehdr - EI_NIDENT))
80 perror ("reading header");
85 sz_phdr = is_e32() ? sizeof(Elf32_Phdr) : sizeof(Elf64_Phdr);
86 if (EHDR_PS(e_phentsize) != sz_phdr)
88 fprintf(stderr, "section size was read as %d, not %d!\n",
89 (int)EHDR_PS(e_phentsize), (int)sz_phdr);
97 elf_find_dynamic_section(int fd, Elf_Ehdr *ehdr, Elf_Phdr *phdr)
100 if (lseek(fd, EHDR_PU(e_phoff), SEEK_SET) == -1)
102 perror ("positioning for sections");
106 for (i = 0; i < EHDR_PS(e_phnum); i++)
108 const size_t sz_phdr = is_e32() ? sizeof(Elf32_Phdr) : sizeof(Elf64_Phdr);
109 if (read(fd, phdr, sz_phdr) != (ssize_t)sz_phdr)
111 perror ("reading section header");
114 if (PHDR_PU(p_type) == PT_DYNAMIC)
117 if (i == EHDR_PS(e_phnum))
119 fprintf (stderr, "No dynamic section found.\n");
123 if (0 == PHDR_PU(p_filesz))
125 fprintf (stderr, "Length of dynamic section is zero.\n");
145 #if defined(DT_RUNPATH)
149 #endif /* DT_RUNPATH */
155 elf_dynpath_tag(int tag)
157 return ( tag == DT_RPATH
158 #if defined(DT_RUNPATH)
160 #endif /* DT_RUNPATH */