getarsym.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * getarsym.c - implementation of the elf_getarsym(3) function.
  3. * Copyright (C) 1995 - 1998, 2004 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. #include <byteswap.h>
  21. #ifndef lint
  22. static const char rcsid[] = "@(#) $Id: getarsym.c,v 1.9 2008/05/23 08:15:34 michael Exp $";
  23. #endif /* lint */
  24. Elf_Arsym*
  25. elf_getarsym(Elf *elf, size_t *ptr) {
  26. Elf_Arsym *syms;
  27. size_t count;
  28. size_t tmp;
  29. size_t i;
  30. char *s;
  31. char *e;
  32. if (!ptr) {
  33. ptr = &tmp;
  34. }
  35. *ptr = 0;
  36. if (!elf) {
  37. return NULL;
  38. }
  39. elf_assert(elf->e_magic == ELF_MAGIC);
  40. if (elf->e_kind != ELF_K_AR) {
  41. seterr(ERROR_NOTARCHIVE);
  42. return NULL;
  43. }
  44. if (elf->e_symtab && !elf->e_free_syms) {
  45. if (elf->e_symlen < 4) {
  46. seterr(ERROR_SIZE_ARSYMTAB);
  47. return NULL;
  48. }
  49. count = __load_u32M(elf->e_symtab);
  50. if (elf->e_symlen < 4 * (count + 1)) {
  51. seterr(ERROR_SIZE_ARSYMTAB);
  52. return NULL;
  53. }
  54. if (!(syms = (Elf_Arsym*)malloc((count + 1) * sizeof(*syms)))) {
  55. seterr(ERROR_MEM_ARSYMTAB);
  56. return NULL;
  57. }
  58. s = elf->e_symtab + 4 * (count + 1);
  59. e = elf->e_symtab + elf->e_symlen;
  60. for (i = 0; i < count; i++, s++) {
  61. syms[i].as_name = s;
  62. while (s < e && *s) {
  63. s++;
  64. }
  65. if (s >= e) {
  66. seterr(ERROR_SIZE_ARSYMTAB);
  67. free(syms);
  68. return NULL;
  69. }
  70. elf_assert(!*s);
  71. syms[i].as_hash = elf_hash((unsigned char*)syms[i].as_name);
  72. syms[i].as_off = __load_u32M(elf->e_symtab + 4 * (i + 1));
  73. }
  74. syms[count].as_name = NULL;
  75. syms[count].as_hash = ~0UL;
  76. syms[count].as_off = 0;
  77. elf->e_symtab = (char*)syms;
  78. elf->e_symlen = count + 1;
  79. elf->e_free_syms = 1;
  80. }
  81. *ptr = elf->e_symlen;
  82. return (Elf_Arsym*)elf->e_symtab;
  83. }