rawfile.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * rawfile.c - implementation of the elf_rawfile(3) function.
  3. * Copyright (C) 1995 - 2009 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: rawfile.c,v 1.8 2009/05/22 17:07:46 michael Exp $";
  22. #endif /* lint */
  23. char*
  24. elf_rawfile(Elf *elf, size_t *ptr) {
  25. size_t tmp;
  26. if (!ptr) {
  27. ptr = &tmp;
  28. }
  29. *ptr = 0;
  30. if (!elf) {
  31. return NULL;
  32. }
  33. elf_assert(elf->e_magic == ELF_MAGIC);
  34. if (!elf->e_readable) {
  35. return NULL;
  36. }
  37. else if (elf->e_size) {
  38. if (!elf->e_rawdata) {
  39. elf_assert(elf->e_data);
  40. if (!elf->e_cooked) {
  41. elf->e_rawdata = elf->e_data;
  42. }
  43. else if (!(elf->e_rawdata = _elf_read(elf, NULL, 0, elf->e_size))) {
  44. return NULL;
  45. }
  46. }
  47. *ptr = elf->e_size;
  48. }
  49. return elf->e_rawdata;
  50. }