00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <private.h>
00025
00026 #ifndef lint
00027 static const char rcsid[] = "@(#) $Id: input.c,v 1.1.1.1 2005/10/21 19:00:00 marcel Exp $";
00028 #endif
00029
00030 #if HAVE_MMAP
00031 #include <sys/mman.h>
00032 #endif
00033
00034 void*
00035 _elf_read(Elf *elf, void *buffer, size_t off, size_t len) {
00036 void *tmp;
00037
00038 elf_assert(elf);
00039 elf_assert(elf->e_magic == ELF_MAGIC);
00040 elf_assert(off >= 0 && off + len <= elf->e_size);
00041 if (elf->e_disabled) {
00042 seterr(ERROR_FDDISABLED);
00043 }
00044 else if (len) {
00045 off += elf->e_base;
00046 if (lseek(elf->e_fd, (off_t)off, SEEK_SET) != (off_t)off) {
00047 seterr(ERROR_IO_SEEK);
00048 }
00049 else if (!(tmp = buffer) && !(tmp = malloc(len))) {
00050 seterr(ERROR_IO_2BIG);
00051 }
00052 else if (read(elf->e_fd, tmp, len) != (int)len) {
00053 seterr(ERROR_IO_READ);
00054 if (tmp != buffer) {
00055 free(tmp);
00056 }
00057 }
00058 else {
00059 return tmp;
00060 }
00061 }
00062 return NULL;
00063 }
00064
00065 void*
00066 _elf_mmap(Elf *elf) {
00067 #if HAVE_MMAP
00068 void *tmp;
00069
00070 elf_assert(elf);
00071 elf_assert(elf->e_magic == ELF_MAGIC);
00072 elf_assert(elf->e_base == 0);
00073 if (elf->e_disabled) {
00074 seterr(ERROR_FDDISABLED);
00075 }
00076 else if (elf->e_size) {
00077 tmp = (void*)mmap(0, elf->e_size, PROT_READ | PROT_WRITE,
00078 MAP_PRIVATE, elf->e_fd, 0);
00079 if (tmp != (void*)-1) {
00080 return tmp;
00081 }
00082 }
00083 #endif
00084 return NULL;
00085 }