input.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * input.c - low-level input for libelf.
  3. * Copyright (C) 1995 - 2001, 2005 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <private.h>
  20. #ifndef lint
  21. static const char rcsid[] = "@(#) $Id: input.c,v 1.11 2008/05/23 08:15:35 michael Exp $";
  22. #endif /* lint */
  23. #include <errno.h>
  24. #if HAVE_MMAP
  25. #include <sys/mman.h>
  26. #endif /* HAVE_MMAP */
  27. static int
  28. xread(int fd, char *buffer, size_t len) {
  29. size_t done = 0;
  30. size_t n;
  31. while (done < len) {
  32. n = read(fd, buffer + done, len - done);
  33. if (n == 0) {
  34. /* premature end of file */
  35. return -1;
  36. }
  37. else if (n != (size_t)-1) {
  38. /* some bytes read, continue */
  39. done += n;
  40. }
  41. else if (errno != EAGAIN && errno != EINTR) {
  42. /* real error */
  43. return -1;
  44. }
  45. }
  46. return 0;
  47. }
  48. void*
  49. _elf_read(Elf *elf, void *buffer, size_t off, size_t len) {
  50. void *tmp;
  51. elf_assert(elf);
  52. elf_assert(elf->e_magic == ELF_MAGIC);
  53. elf_assert(off >= 0 && off + len <= elf->e_size);
  54. if (elf->e_disabled) {
  55. seterr(ERROR_FDDISABLED);
  56. }
  57. else if (len) {
  58. off += elf->e_base;
  59. if (lseek(elf->e_fd, (off_t)off, SEEK_SET) != (off_t)off) {
  60. seterr(ERROR_IO_SEEK);
  61. }
  62. else if (!(tmp = buffer) && !(tmp = malloc(len))) {
  63. seterr(ERROR_IO_2BIG);
  64. }
  65. else if (xread(elf->e_fd, tmp, len)) {
  66. seterr(ERROR_IO_READ);
  67. if (tmp != buffer) {
  68. free(tmp);
  69. }
  70. }
  71. else {
  72. return tmp;
  73. }
  74. }
  75. return NULL;
  76. }
  77. void*
  78. _elf_mmap(Elf *elf) {
  79. #if HAVE_MMAP
  80. void *tmp;
  81. elf_assert(elf);
  82. elf_assert(elf->e_magic == ELF_MAGIC);
  83. elf_assert(elf->e_base == 0);
  84. if (elf->e_disabled) {
  85. seterr(ERROR_FDDISABLED);
  86. }
  87. else if (elf->e_size) {
  88. tmp = (void*)mmap(0, elf->e_size, PROT_READ | PROT_WRITE,
  89. MAP_PRIVATE, elf->e_fd, 0);
  90. if (tmp != (void*)-1) {
  91. return tmp;
  92. }
  93. }
  94. #endif /* HAVE_MMAP */
  95. return NULL;
  96. }