nlist.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * nlist.c - implementation of the nlist(3) function.
  3. * Copyright (C) 1995 - 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 <nlist.h>
  21. #ifndef lint
  22. static const char rcsid[] = "@(#) $Id: nlist.c,v 1.15 2008/05/23 08:15:35 michael Exp $";
  23. #endif /* lint */
  24. #if !defined(_WIN32)
  25. #if HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #else
  28. extern int open();
  29. #endif /* HAVE_FCNTL_H */
  30. #endif /* defined(_WIN32) */
  31. #ifndef O_RDONLY
  32. #define O_RDONLY 0
  33. #endif /* O_RDONLY */
  34. #ifndef O_BINARY
  35. #define O_BINARY 0
  36. #endif /* O_BINARY */
  37. #define FILE_OPEN_MODE (O_RDONLY | O_BINARY)
  38. #define PRIME 217
  39. struct hash {
  40. const char* name;
  41. unsigned long hash;
  42. unsigned next;
  43. };
  44. static const char*
  45. symbol_name(Elf *elf, const void *syms, const char *names, size_t nlimit, size_t index) {
  46. size_t off;
  47. if (elf->e_class == ELFCLASS32) {
  48. off = ((Elf32_Sym*)syms)[index].st_name;
  49. }
  50. #if __LIBELF64
  51. else if (elf->e_class == ELFCLASS64) {
  52. off = ((Elf64_Sym*)syms)[index].st_name;
  53. }
  54. #endif /* __LIBELF64 */
  55. else {
  56. return NULL;
  57. }
  58. if (off >= 0 && off < nlimit) {
  59. return &names[off];
  60. }
  61. return NULL;
  62. }
  63. static void
  64. copy_symbol(Elf *elf, struct nlist *np, const void *syms, size_t index) {
  65. if (elf->e_class == ELFCLASS32) {
  66. np->n_value = ((Elf32_Sym*)syms)[index].st_value;
  67. np->n_scnum = ((Elf32_Sym*)syms)[index].st_shndx;
  68. }
  69. #if __LIBELF64
  70. else if (elf->e_class == ELFCLASS64) {
  71. np->n_value = ((Elf64_Sym*)syms)[index].st_value;
  72. np->n_scnum = ((Elf64_Sym*)syms)[index].st_shndx;
  73. }
  74. #endif /* __LIBELF64 */
  75. /*
  76. * this needs more work
  77. */
  78. np->n_type = 0;
  79. np->n_sclass = 0;
  80. np->n_numaux = 0;
  81. }
  82. static int
  83. _elf_nlist(Elf *elf, struct nlist *nl) {
  84. unsigned first[PRIME];
  85. Elf_Scn *symtab = NULL;
  86. Elf_Scn *strtab = NULL;
  87. Elf_Data *symdata;
  88. Elf_Data *strdata;
  89. size_t symsize;
  90. size_t nsymbols;
  91. const char *name;
  92. struct hash *table;
  93. unsigned long hash;
  94. unsigned i;
  95. struct nlist *np;
  96. /*
  97. * Get and translate ELF header, section table and so on.
  98. * Must be class independent, so don't use elf32_get*().
  99. */
  100. if (elf->e_kind != ELF_K_ELF) {
  101. return -1;
  102. }
  103. if (!elf->e_ehdr && !_elf_cook(elf)) {
  104. return -1;
  105. }
  106. /*
  107. * Find symbol table. If there is none, try dynamic symbols.
  108. */
  109. for (symtab = elf->e_scn_1; symtab; symtab = symtab->s_link) {
  110. if (symtab->s_type == SHT_SYMTAB) {
  111. break;
  112. }
  113. if (symtab->s_type == SHT_DYNSYM) {
  114. strtab = symtab;
  115. }
  116. }
  117. if (!symtab && !(symtab = strtab)) {
  118. return -1;
  119. }
  120. /*
  121. * Get associated string table.
  122. */
  123. i = 0;
  124. if (elf->e_class == ELFCLASS32) {
  125. i = symtab->s_shdr32.sh_link;
  126. }
  127. #if __LIBELF64
  128. else if (elf->e_class == ELFCLASS64) {
  129. i = symtab->s_shdr64.sh_link;
  130. }
  131. #endif /* __LIBELF64 */
  132. if (i == 0) {
  133. return -1;
  134. }
  135. for (strtab = elf->e_scn_1; strtab; strtab = strtab->s_link) {
  136. if (strtab->s_index == i) {
  137. break;
  138. }
  139. }
  140. if (!strtab || strtab->s_type != SHT_STRTAB) {
  141. return -1;
  142. }
  143. /*
  144. * Get and translate section data.
  145. */
  146. symdata = elf_getdata(symtab, NULL);
  147. strdata = elf_getdata(strtab, NULL);
  148. if (!symdata || !strdata) {
  149. return -1;
  150. }
  151. symsize = _msize(elf->e_class, _elf_version, ELF_T_SYM);
  152. elf_assert(symsize);
  153. nsymbols = symdata->d_size / symsize;
  154. if (!symdata->d_buf || !strdata->d_buf || !nsymbols || !strdata->d_size) {
  155. return -1;
  156. }
  157. /*
  158. * Build a simple hash table.
  159. */
  160. if (!(table = (struct hash*)malloc(nsymbols * sizeof(*table)))) {
  161. return -1;
  162. }
  163. for (i = 0; i < PRIME; i++) {
  164. first[i] = 0;
  165. }
  166. for (i = 0; i < nsymbols; i++) {
  167. table[i].name = NULL;
  168. table[i].hash = 0;
  169. table[i].next = 0;
  170. }
  171. for (i = 1; i < nsymbols; i++) {
  172. name = symbol_name(elf, symdata->d_buf, strdata->d_buf,
  173. strdata->d_size, i);
  174. if (name == NULL) {
  175. free(table);
  176. return -1;
  177. }
  178. if (*name != '\0') {
  179. table[i].name = name;
  180. table[i].hash = elf_hash((unsigned char*)name);
  181. hash = table[i].hash % PRIME;
  182. table[i].next = first[hash];
  183. first[hash] = i;
  184. }
  185. }
  186. /*
  187. * Lookup symbols, one by one.
  188. */
  189. for (np = nl; (name = np->n_name) && *name; np++) {
  190. hash = elf_hash((unsigned char*)name);
  191. for (i = first[hash % PRIME]; i; i = table[i].next) {
  192. if (table[i].hash == hash && !strcmp(table[i].name, name)) {
  193. break;
  194. }
  195. }
  196. if (i) {
  197. copy_symbol(elf, np, symdata->d_buf, i);
  198. }
  199. else {
  200. np->n_value = 0;
  201. np->n_scnum = 0;
  202. np->n_type = 0;
  203. np->n_sclass = 0;
  204. np->n_numaux = 0;
  205. }
  206. }
  207. free(table);
  208. return 0;
  209. }
  210. int
  211. nlist(const char *filename, struct nlist *nl) {
  212. int result = -1;
  213. unsigned oldver;
  214. Elf *elf;
  215. int fd;
  216. if ((oldver = elf_version(EV_CURRENT)) != EV_NONE) {
  217. if ((fd = open(filename, FILE_OPEN_MODE)) != -1) {
  218. if ((elf = elf_begin(fd, ELF_C_READ, NULL))) {
  219. result = _elf_nlist(elf, nl);
  220. elf_end(elf);
  221. }
  222. close(fd);
  223. }
  224. elf_version(oldver);
  225. }
  226. if (result) {
  227. while (nl->n_name && *nl->n_name) {
  228. nl->n_value = 0;
  229. nl++;
  230. }
  231. }
  232. return result;
  233. }