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