rawdata.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. rawdata.c - implementation of the elf_rawdata(3) function.
  3. Copyright (C) 1995 - 2000 Michael Riepe
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  15. */
  16. #include <private.h>
  17. #ifndef lint
  18. static const char rcsid[] = "@(#) $Id: rawdata.c,v 1.10 2008/05/23 08:15:35 michael Exp $";
  19. #endif /* lint */
  20. Elf_Data*
  21. elf_rawdata(Elf_Scn *scn, Elf_Data *data) {
  22. Scn_Data *sd;
  23. Elf *elf;
  24. if (!scn) {
  25. return NULL;
  26. }
  27. elf_assert(scn->s_magic == SCN_MAGIC);
  28. elf = scn->s_elf;
  29. elf_assert(elf);
  30. elf_assert(elf->e_magic == ELF_MAGIC);
  31. if (!elf->e_readable) {
  32. return NULL;
  33. }
  34. else if (scn->s_index == SHN_UNDEF || scn->s_type == SHT_NULL) {
  35. seterr(ERROR_NULLSCN);
  36. }
  37. else if (data) {
  38. return NULL;
  39. }
  40. else if ((sd = scn->s_rawdata)) {
  41. elf_assert(sd->sd_magic == DATA_MAGIC);
  42. elf_assert(sd->sd_scn == scn);
  43. return &sd->sd_data;
  44. }
  45. else if (scn->s_offset < 0 || scn->s_offset > elf->e_size) {
  46. seterr(ERROR_OUTSIDE);
  47. }
  48. else if (scn->s_type != SHT_NOBITS
  49. && scn->s_offset + scn->s_size > elf->e_size) {
  50. seterr(ERROR_TRUNC_SCN);
  51. }
  52. else if (!(sd = (Scn_Data*)malloc(sizeof(*sd)))) {
  53. seterr(ERROR_MEM_SCNDATA);
  54. }
  55. else {
  56. *sd = _elf_data_init;
  57. sd->sd_scn = scn;
  58. sd->sd_freeme = 1;
  59. sd->sd_data.d_size = scn->s_size;
  60. sd->sd_data.d_version = _elf_version;
  61. if (scn->s_type != SHT_NOBITS && scn->s_size) {
  62. if (!(sd->sd_memdata = (char*)malloc(scn->s_size))) {
  63. seterr(ERROR_IO_2BIG);
  64. free(sd);
  65. return NULL;
  66. }
  67. else if (elf->e_rawdata) {
  68. memcpy(sd->sd_memdata, elf->e_rawdata + scn->s_offset, scn->s_size);
  69. }
  70. else if (!_elf_read(elf, sd->sd_memdata, scn->s_offset, scn->s_size)) {
  71. free(sd->sd_memdata);
  72. free(sd);
  73. return NULL;
  74. }
  75. sd->sd_data.d_buf = sd->sd_memdata;
  76. sd->sd_free_data = 1;
  77. }
  78. scn->s_rawdata = sd;
  79. return &sd->sd_data;
  80. }
  81. return NULL;
  82. }