1 /* Extract symbol list from binary.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
48 #define TYPE struct hashentry
49 /* XXX Use a better hash function some day. */
50 #define HASHFCT(str, len) INTUSE(elf_hash) (str)
51 #define COMPARE(p1, p2) strcmp ((p1)->str, (p2)->str)
54 #define xcalloc(n, m) calloc (n, m)
55 #define next_prime(s) __libelf_next_prime (s)
56 #include "fixedsizehash.h"
60 nlist (const char *filename
, struct nlist
*nl
)
65 Elf_Scn
*symscn
= NULL
;
67 GElf_Shdr
*shdr
= NULL
;
69 struct nlist_fshash
*table
;
74 fd
= open (filename
, O_RDONLY
);
77 __libelf_seterrno (ELF_E_NOFILE
);
81 /* For compatibility reasons (`nlist' existed before ELF and libelf)
82 we don't expect the caller to set the ELF version. Do this here
83 if it hasn't happened yet. */
84 if (__libelf_version_initialized
== 0)
85 INTUSE(elf_version
) (EV_CURRENT
);
87 /* Now get an ELF descriptor. */
88 elf
= INTUSE(elf_begin
) (fd
, ELF_C_READ_MMAP
, NULL
);
92 /* Find a symbol table. We prefer the real symbol table but if it
93 does not exist use the dynamic symbol table. */
94 while ((scn
= INTUSE(elf_nextscn
) (elf
, scn
)) != NULL
)
96 shdr
= INTUSE(gelf_getshdr
) (scn
, &shdr_mem
);
100 /* That is what we are looking for. */
101 if (shdr
->sh_type
== SHT_SYMTAB
)
107 /* Better than nothing. Remember this section. */
108 if (shdr
->sh_type
== SHT_DYNSYM
)
113 /* We haven't found anything. Fail. */
116 /* Re-get the section header in case we found only the dynamic symbol
119 shdr
= INTUSE(gelf_getshdr
) (symscn
, &shdr_mem
);
120 /* SHDR->SH_LINK now contains the index of the string section. */
122 /* Get the data for the symbol section. */
123 data
= INTUSE(elf_getdata
) (symscn
, NULL
);
127 /* How many symbols are there? */
128 nsyms
= (shdr
->sh_size
129 / INTUSE(gelf_fsize
) (elf
, ELF_T_SYM
, 1, data
->d_version
));
131 /* Create the hash table. */
132 table
= nlist_fshash_init (nsyms
);
135 __libelf_seterrno (ELF_E_NOMEM
);
139 /* Iterate over all the symbols in the section. */
140 for (cnt
= 0; cnt
< nsyms
; ++cnt
)
142 struct hashentry mem
;
145 /* Get the symbol. */
146 sym
= INTUSE(gelf_getsym
) (data
, cnt
, &mem
.sym
);
150 /* Get the name of the symbol. */
151 mem
.str
= INTUSE(elf_strptr
) (elf
, shdr
->sh_link
, sym
->st_name
);
155 /* Don't allow zero-length strings. */
156 if (mem
.str
[0] == '\0')
159 /* And add it to the hash table. Note that we are using the
160 overwrite version. This will ensure that
161 a) global symbols are preferred over local symbols since
162 they are all located at the end
163 b) if there are multiple local symbols with the same name
164 the last one is used.
166 (void) nlist_fshash_overwrite (table
, mem
.str
, 0, &mem
);
169 /* Now it is time to look for the symbols the user asked for.
170 XXX What is a `null name/null string'? This is what the
171 standard says terminates the list. Is it a null pointer
172 or a zero-length string? We test for both... */
173 while (nl
->n_name
!= NULL
&& nl
->n_name
[0] != '\0')
175 struct hashentry search
;
176 const struct hashentry
*found
;
178 /* Search for a matching entry in the hash table. */
179 search
.str
= nl
->n_name
;
180 found
= nlist_fshash_find (table
, nl
->n_name
, 0, &search
);
185 nl
->n_value
= found
->sym
.st_value
;
186 nl
->n_scnum
= found
->sym
.st_shndx
;
187 nl
->n_type
= GELF_ST_TYPE (found
->sym
.st_info
);
188 /* XXX What shall we fill in the next fields? */
202 /* Next search request. */
206 /* Free the resources. */
207 nlist_fshash_fini (table
);
209 /* We do not need the ELF descriptor anymore. */
210 (void) INTUSE(elf_end
) (elf
);
212 /* Neither the file descriptor. */
218 nlist_fshash_fini (table
);
221 /* We do not need the ELF descriptor anymore. */
222 (void) INTUSE(elf_end
) (elf
);
225 /* Neither the file descriptor. */
229 /* We have to set all entries to zero. */
230 while (nl
->n_name
!= NULL
&& nl
->n_name
[0] != '\0')