1 /* Return symbol table of archive.
2 Copyright (C) 1998, 1999, 2000, 2002, 2005 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
70 elf_getarsym (elf, ptr)
74 if (elf->kind != ELF_K_AR)
76 /* This is no archive. */
77 __libelf_seterrno (ELF_E_NO_ARCHIVE);
82 /* In case of an error or when we know the value store the expected
83 value now. Doing this allows us easier exits in an error case. */
84 *ptr = elf->state.ar.ar_sym_num;
86 if (elf->state.ar.ar_sym == (Elf_Arsym *) -1l)
88 /* There is no index. */
89 __libelf_seterrno (ELF_E_NO_INDEX);
93 Elf_Arsym *result = elf->state.ar.ar_sym;
96 /* We have not yet read the index. */
97 rwlock_wrlock (elf->lock);
99 /* In case we find no index remember this for the next call. */
100 elf->state.ar.ar_sym = (Elf_Arsym *) -1l;
102 struct ar_hdr *index_hdr;
103 if (elf->map_address == NULL)
105 /* We must read index from the file. */
106 assert (elf->fildes != -1);
107 if (pread_retry (elf->fildes, &elf->state.ar.ar_hdr,
108 sizeof (struct ar_hdr), elf->start_offset + SARMAG)
109 != sizeof (struct ar_hdr))
111 /* It is not possible to read the index. Maybe it does not
113 __libelf_seterrno (ELF_E_READ_ERROR);
117 index_hdr = &elf->state.ar.ar_hdr;
121 if (SARMAG + sizeof (struct ar_hdr) > elf->maximum_size)
123 /* There is no room for the full archive. */
124 __libelf_seterrno (ELF_E_NO_INDEX);
128 index_hdr = (struct ar_hdr *) (elf->map_address
129 + elf->start_offset + SARMAG);
132 /* Now test whether this really is an archive. */
133 if (memcmp (index_hdr->ar_fmag, ARFMAG, 2) != 0)
135 /* Invalid magic bytes. */
136 __libelf_seterrno (ELF_E_ARCHIVE_FMAG);
140 /* Now test whether this is the index. It is denoted by the
142 XXX This is not entirely true. There are some more forms.
143 Which of them shall we handle? */
144 if (memcmp (index_hdr->ar_name, "/ ", 16) != 0)
146 /* If the index is not the first entry, there is no index.
149 __libelf_seterrno (ELF_E_NO_INDEX);
153 /* We have an archive. The first word in there is the number of
154 entries in the table. */
156 if (elf->map_address == NULL)
158 if (pread_retry (elf->fildes, &n, sizeof (n),
159 elf->start_offset + SARMAG + sizeof (struct ar_hdr))
162 /* Cannot read the number of entries. */
163 __libelf_seterrno (ELF_E_NO_INDEX);
168 n = *(uint32_t *) (elf->map_address + elf->start_offset
169 + SARMAG + sizeof (struct ar_hdr));
171 if (__BYTE_ORDER == __LITTLE_ENDIAN)
174 /* Now we can perform some first tests on whether all the data
175 needed for the index is available. */
177 memcpy (tmpbuf, index_hdr->ar_size, 10);
179 size_t index_size = atol (tmpbuf);
181 if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
182 || n * sizeof (uint32_t) > index_size)
184 /* This index table cannot be right since it does not fit into
186 __libelf_seterrno (ELF_E_NO_INDEX);
190 /* Now we can allocate the arrays needed to store the index. */
191 size_t ar_sym_len = (n + 1) * sizeof (Elf_Arsym);
192 elf->state.ar.ar_sym = (Elf_Arsym *) malloc (ar_sym_len);
193 if (elf->state.ar.ar_sym != NULL)
198 if (elf->map_address == NULL)
200 file_data = (uint32_t *) alloca (n * sizeof (uint32_t));
202 ar_sym_len += index_size - n * sizeof (uint32_t);
203 Elf_Arsym *newp = (Elf_Arsym *) realloc (elf->state.ar.ar_sym,
207 free (elf->state.ar.ar_sym);
208 elf->state.ar.ar_sym = NULL;
209 __libelf_seterrno (ELF_E_NOMEM);
212 elf->state.ar.ar_sym = newp;
214 char *new_str = (char *) (elf->state.ar.ar_sym + n + 1);
216 /* Now read the data from the file. */
217 if ((size_t) pread_retry (elf->fildes, file_data,
218 n * sizeof (uint32_t),
219 elf->start_offset + SARMAG
220 + sizeof (struct ar_hdr)
222 != n * sizeof (uint32_t)
223 || ((size_t) pread_retry (elf->fildes, new_str,
224 index_size - n * sizeof (uint32_t),
226 + SARMAG + sizeof (struct ar_hdr)
227 + (n + 1) * sizeof (uint32_t))
228 != index_size - n * sizeof (uint32_t)))
230 /* We were not able to read the data. */
231 free (elf->state.ar.ar_sym);
232 elf->state.ar.ar_sym = NULL;
233 __libelf_seterrno (ELF_E_NO_INDEX);
237 str_data = (char *) new_str;
241 file_data = (uint32_t *) (elf->map_address + elf->start_offset
242 + SARMAG + sizeof (struct ar_hdr)
243 + sizeof (uint32_t));
244 str_data = (char *) &file_data[n];
247 /* Now we can build the data structure. */
248 Elf_Arsym *arsym = elf->state.ar.ar_sym;
249 for (size_t cnt = 0; cnt < n; ++cnt)
251 arsym[cnt].as_name = str_data;
252 if (__BYTE_ORDER == __LITTLE_ENDIAN)
253 arsym[cnt].as_off = bswap_32 (file_data[cnt]);
255 arsym[cnt].as_off = file_data[cnt];
256 arsym[cnt].as_hash = _dl_elf_hash (str_data);
257 str_data = rawmemchr (str_data, '\0') + 1;
259 /* At the end a special entry. */
260 arsym[n].as_name = NULL;
262 arsym[n].as_hash = ~0UL;
264 /* Tell the caller how many entries we have. */
265 elf->state.ar.ar_sym_num = n + 1;
268 result = elf->state.ar.ar_sym;
271 rwlock_unlock (elf->lock);
275 *ptr = elf->state.ar.ar_sym_num;