1260684Skaiw/*-
2260684Skaiw * Copyright (c) 2006,2008,2010 Joseph Koshy
3260684Skaiw * All rights reserved.
4260684Skaiw *
5260684Skaiw * Redistribution and use in source and binary forms, with or without
6260684Skaiw * modification, are permitted provided that the following conditions
7260684Skaiw * are met:
8260684Skaiw * 1. Redistributions of source code must retain the above copyright
9260684Skaiw *    notice, this list of conditions and the following disclaimer.
10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
11260684Skaiw *    notice, this list of conditions and the following disclaimer in the
12260684Skaiw *    documentation and/or other materials provided with the distribution.
13260684Skaiw *
14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17260684Skaiw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24260684Skaiw * SUCH DAMAGE.
25260684Skaiw */
26260684Skaiw
27260684Skaiw#include <assert.h>
28260684Skaiw#include <ctype.h>
29260684Skaiw#include <libelf.h>
30260684Skaiw#include <stdlib.h>
31260684Skaiw#include <string.h>
32260684Skaiw
33260684Skaiw#include "_libelf.h"
34260684Skaiw#include "_libelf_ar.h"
35260684Skaiw
36367466SdimELFTC_VCSID("$Id: libelf_ar.c 3712 2019-03-16 22:23:34Z jkoshy $");
37260684Skaiw
38260684Skaiw#define	LIBELF_NALLOC_SIZE	16
39260684Skaiw
40260684Skaiw/*
41260684Skaiw * `ar' archive handling.
42260684Skaiw *
43260684Skaiw * `ar' archives start with signature `ARMAG'.  Each archive member is
44260684Skaiw * preceded by a header containing meta-data for the member.  This
45260684Skaiw * header is described in <ar.h> (struct ar_hdr).  The header always
46260684Skaiw * starts on an even address.  File data is padded with "\n"
47260684Skaiw * characters to keep this invariant.
48260684Skaiw *
49260684Skaiw * Special considerations for `ar' archives:
50260684Skaiw *
51260684Skaiw * There are two variants of the `ar' archive format: traditional BSD
52260684Skaiw * and SVR4.  These differ in the way long file names are treated, and
53260684Skaiw * in the layout of the archive symbol table.
54260684Skaiw *
55260684Skaiw * The `ar' header only has space for a 16 character file name.
56260684Skaiw *
57260684Skaiw * In the SVR4 format, file names are terminated with a '/', so this
58260684Skaiw * effectively leaves 15 characters for the actual file name.  Longer
59260684Skaiw * file names stored in a separate 'string table' and referenced
60260684Skaiw * indirectly from the name field.  The string table itself appears as
61260684Skaiw * an archive member with name "// ".  An `indirect' file name in an
62260684Skaiw * `ar' header matches the pattern "/[0-9]*". The digits form a
63260684Skaiw * decimal number that corresponds to a byte offset into the string
64260684Skaiw * table where the actual file name of the object starts.  Strings in
65260684Skaiw * the string table are padded to start on even addresses.
66260684Skaiw *
67300311Semaste * In the BSD format, file names can be up to 16 characters.  File
68260684Skaiw * names shorter than 16 characters are padded to 16 characters using
69260684Skaiw * (ASCII) space characters.  File names with embedded spaces and file
70260684Skaiw * names longer than 16 characters are stored immediately after the
71260684Skaiw * archive header and the name field set to a special indirect name
72260684Skaiw * matching the pattern "#1/[0-9]+".  The digits form a decimal number
73260684Skaiw * that corresponds to the actual length of the file name following
74260684Skaiw * the archive header.  The content of the archive member immediately
75260684Skaiw * follows the file name, and the size field of the archive member
76260684Skaiw * holds the sum of the sizes of the member and of the appended file
77260684Skaiw * name.
78260684Skaiw *
79260684Skaiw * Archives may also have a symbol table (see ranlib(1)), mapping
80260684Skaiw * program symbols to object files inside the archive.
81260684Skaiw *
82260684Skaiw * In the SVR4 format, a symbol table uses a file name of "/ " in its
83260684Skaiw * archive header.  The symbol table is structured as:
84260684Skaiw *  - a 4-byte count of entries stored as a binary value, MSB first
85260684Skaiw *  - 'n' 4-byte offsets, stored as binary values, MSB first
86260684Skaiw *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
87260684Skaiw *
88260684Skaiw * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
89260684Skaiw * It is structured as two parts:
90260684Skaiw *  - The first part is an array of "ranlib" structures preceded by
91260684Skaiw *    the size of the array in bytes.  Each "ranlib" structure
92260684Skaiw *    describes one symbol.  Each structure contains an offset into
93260684Skaiw *    the string table for the symbol name, and a file offset into the
94260684Skaiw *    archive for the member defining the symbol.
95260684Skaiw *  - The second part is a string table containing NUL-terminated
96260684Skaiw *    strings, preceded by the size of the string table in bytes.
97260684Skaiw *
98260684Skaiw * If the symbol table and string table are is present in an archive
99260684Skaiw * they must be the very first objects and in that order.
100260684Skaiw */
101260684Skaiw
102260684Skaiw
103260684Skaiw/*
104260684Skaiw * Retrieve an archive header descriptor.
105260684Skaiw */
106260684Skaiw
107260684SkaiwElf_Arhdr *
108260684Skaiw_libelf_ar_gethdr(Elf *e)
109260684Skaiw{
110260684Skaiw	Elf *parent;
111276371Semaste	Elf_Arhdr *eh;
112260684Skaiw	char *namelen;
113260684Skaiw	size_t n, nlen;
114260684Skaiw	struct ar_hdr *arh;
115260684Skaiw
116260684Skaiw	if ((parent = e->e_parent) == NULL) {
117260684Skaiw		LIBELF_SET_ERROR(ARGUMENT, 0);
118260684Skaiw		return (NULL);
119260684Skaiw	}
120260684Skaiw
121260684Skaiw	assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
122260684Skaiw
123260684Skaiw	arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
124260684Skaiw
125260684Skaiw	assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
126260684Skaiw
127367466Sdim	/*
128367466Sdim	 * There needs to be enough space remaining in the file for the
129367466Sdim	 * archive header.
130367466Sdim	 */
131367466Sdim	if ((uintptr_t) arh > (uintptr_t) parent->e_rawfile +
132367466Sdim	    (uintptr_t) parent->e_rawsize - sizeof(struct ar_hdr)) {
133367466Sdim		LIBELF_SET_ERROR(ARCHIVE, 0);
134367466Sdim		return (NULL);
135367466Sdim	}
136367466Sdim
137260684Skaiw	if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
138260684Skaiw		LIBELF_SET_ERROR(RESOURCE, 0);
139260684Skaiw		return (NULL);
140260684Skaiw	}
141260684Skaiw
142260684Skaiw	e->e_hdr.e_arhdr = eh;
143260684Skaiw	e->e_flags |= LIBELF_F_AR_HEADER;
144260684Skaiw
145260684Skaiw	eh->ar_name = eh->ar_rawname = NULL;
146260684Skaiw
147260684Skaiw	if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
148260684Skaiw	    NULL)
149260684Skaiw		goto error;
150260684Skaiw
151260684Skaiw	if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
152260684Skaiw	    &n) == 0)
153260684Skaiw		goto error;
154260684Skaiw	eh->ar_uid = (uid_t) n;
155260684Skaiw
156260684Skaiw	if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
157260684Skaiw	    &n) == 0)
158260684Skaiw		goto error;
159260684Skaiw	eh->ar_gid = (gid_t) n;
160260684Skaiw
161260684Skaiw	if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8,
162260684Skaiw	    &n) == 0)
163260684Skaiw		goto error;
164260684Skaiw	eh->ar_mode = (mode_t) n;
165260684Skaiw
166260684Skaiw	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
167260684Skaiw	    &n) == 0)
168260684Skaiw		goto error;
169260684Skaiw
170260684Skaiw	/*
171260684Skaiw	 * Get the true size of the member if extended naming is being used.
172260684Skaiw	 */
173260684Skaiw	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
174260684Skaiw		namelen = arh->ar_name +
175260684Skaiw		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
176260684Skaiw		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
177260684Skaiw		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
178260684Skaiw			goto error;
179260684Skaiw		n -= nlen;
180260684Skaiw	}
181260684Skaiw
182260684Skaiw	eh->ar_size = n;
183260684Skaiw
184260684Skaiw	if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
185260684Skaiw		goto error;
186260684Skaiw
187260684Skaiw	eh->ar_flags = 0;
188260684Skaiw
189260684Skaiw	return (eh);
190260684Skaiw
191260684Skaiw error:
192260684Skaiw	if (eh) {
193260684Skaiw		if (eh->ar_name)
194260684Skaiw			free(eh->ar_name);
195260684Skaiw		if (eh->ar_rawname)
196260684Skaiw			free(eh->ar_rawname);
197260684Skaiw		free(eh);
198260684Skaiw	}
199260684Skaiw
200260684Skaiw	e->e_flags &= ~LIBELF_F_AR_HEADER;
201276371Semaste	e->e_hdr.e_rawhdr = (unsigned char *) arh;
202260684Skaiw
203260684Skaiw	return (NULL);
204260684Skaiw}
205260684Skaiw
206260684SkaiwElf *
207260684Skaiw_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
208260684Skaiw{
209260684Skaiw	Elf *e;
210260684Skaiw	size_t nsz, sz;
211367466Sdim	off_t next, end;
212260684Skaiw	struct ar_hdr *arh;
213276371Semaste	char *member, *namelen;
214260684Skaiw
215260684Skaiw	assert(elf->e_kind == ELF_K_AR);
216260684Skaiw
217260684Skaiw	next = elf->e_u.e_ar.e_next;
218260684Skaiw
219260684Skaiw	/*
220260684Skaiw	 * `next' is only set to zero by elf_next() when the last
221260684Skaiw	 * member of an archive is processed.
222260684Skaiw	 */
223260684Skaiw	if (next == (off_t) 0)
224260684Skaiw		return (NULL);
225260684Skaiw
226260684Skaiw	assert((next & 1) == 0);
227260684Skaiw
228367466Sdim	/*
229367466Sdim	 * There needs to be enough space in the file to contain an
230367466Sdim	 * ar(1) header.
231367466Sdim	 */
232367466Sdim	end = next + (off_t) sizeof(struct ar_hdr);
233367466Sdim	if ((uintmax_t) end < (uintmax_t) next || /* Overflow. */
234367466Sdim	    end > (off_t) elf->e_rawsize) {
235367466Sdim		LIBELF_SET_ERROR(ARCHIVE, 0);
236367466Sdim		return (NULL);
237367466Sdim	}
238367466Sdim
239260684Skaiw	arh = (struct ar_hdr *) (elf->e_rawfile + next);
240260684Skaiw
241260684Skaiw	/*
242260684Skaiw	 * Retrieve the size of the member.
243260684Skaiw	 */
244260684Skaiw	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
245260684Skaiw	    &sz) == 0) {
246260684Skaiw		LIBELF_SET_ERROR(ARCHIVE, 0);
247260684Skaiw		return (NULL);
248260684Skaiw	}
249260684Skaiw
250260684Skaiw	/*
251367466Sdim	 * Check if the archive member that follows will fit in the
252367466Sdim	 * containing archive.
253367466Sdim	 */
254367466Sdim	end += (off_t) sz;
255367466Sdim	if (end < next || /* Overflow. */
256367466Sdim	    end > (off_t) elf->e_rawsize) {
257367466Sdim		LIBELF_SET_ERROR(ARCHIVE, 0);
258367466Sdim		return (NULL);
259367466Sdim	}
260367466Sdim
261367466Sdim	/*
262260684Skaiw	 * Adjust the size field for members in BSD archives using
263260684Skaiw	 * extended naming.
264260684Skaiw	 */
265260684Skaiw	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
266260684Skaiw		namelen = arh->ar_name +
267260684Skaiw		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
268260684Skaiw		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
269260684Skaiw		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
270260684Skaiw			LIBELF_SET_ERROR(ARCHIVE, 0);
271260684Skaiw			return (NULL);
272260684Skaiw		}
273260684Skaiw
274260684Skaiw		member = (char *) (arh + 1) + nsz;
275260684Skaiw		sz -= nsz;
276260684Skaiw	} else
277260684Skaiw		member = (char *) (arh + 1);
278260684Skaiw
279260684Skaiw
280276371Semaste	if ((e = elf_memory(member, sz)) == NULL)
281260684Skaiw		return (NULL);
282260684Skaiw
283260684Skaiw	e->e_fd = fd;
284260684Skaiw	e->e_cmd = c;
285276371Semaste	e->e_hdr.e_rawhdr = (unsigned char *) arh;
286260684Skaiw
287260684Skaiw	elf->e_u.e_ar.e_nchildren++;
288260684Skaiw	e->e_parent = elf;
289260684Skaiw
290260684Skaiw	return (e);
291260684Skaiw}
292260684Skaiw
293260684Skaiw/*
294260684Skaiw * A BSD-style ar(1) symbol table has the following layout:
295260684Skaiw *
296260684Skaiw * - A count of bytes used by the following array of 'ranlib'
297260684Skaiw *   structures, stored as a 'long'.
298260684Skaiw * - An array of 'ranlib' structures.  Each array element is
299260684Skaiw *   two 'long's in size.
300260684Skaiw * - A count of bytes used for the following symbol table.
301260684Skaiw * - The symbol table itself.
302260684Skaiw */
303260684Skaiw
304260684Skaiw/*
305276371Semaste * A helper macro to read in a 'long' value from the archive.
306276371Semaste *
307276371Semaste * We use memcpy() since the source pointer may be misaligned with
308276371Semaste * respect to the natural alignment for a C 'long'.
309260684Skaiw */
310260684Skaiw#define	GET_LONG(P, V)do {				\
311260684Skaiw		memcpy(&(V), (P), sizeof(long));	\
312260684Skaiw		(P) += sizeof(long);			\
313260684Skaiw	} while (0)
314260684Skaiw
315260684SkaiwElf_Arsym *
316260684Skaiw_libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
317260684Skaiw{
318260684Skaiw	Elf_Arsym *symtab, *sym;
319367466Sdim	unsigned int n;
320367466Sdim	size_t nentries;
321260684Skaiw	unsigned char *end, *p, *p0, *s, *s0;
322276371Semaste	const size_t entrysize = 2 * sizeof(long);
323276371Semaste	long arraysize, fileoffset, stroffset, strtabsize;
324260684Skaiw
325260684Skaiw	assert(e != NULL);
326260684Skaiw	assert(count != NULL);
327260684Skaiw	assert(e->e_u.e_ar.e_symtab == NULL);
328260684Skaiw
329260684Skaiw	symtab = NULL;
330260684Skaiw
331260684Skaiw	/*
332260684Skaiw	 * The BSD symbol table always contains the count fields even
333260684Skaiw	 * if there are no entries in it.
334260684Skaiw	 */
335260684Skaiw	if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
336260684Skaiw		goto symtaberror;
337260684Skaiw
338260684Skaiw	p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
339260684Skaiw	end = p0 + e->e_u.e_ar.e_rawsymtabsz;
340260684Skaiw
341260684Skaiw	/*
342260684Skaiw	 * Retrieve the size of the array of ranlib descriptors and
343260684Skaiw	 * check it for validity.
344260684Skaiw	 */
345260684Skaiw	GET_LONG(p, arraysize);
346260684Skaiw
347276371Semaste	if (arraysize < 0 || p0 + arraysize >= end ||
348276371Semaste	    ((size_t) arraysize % entrysize != 0))
349260684Skaiw		goto symtaberror;
350260684Skaiw
351260684Skaiw	/*
352260684Skaiw	 * Check the value of the string table size.
353260684Skaiw	 */
354260684Skaiw	s = p + arraysize;
355260684Skaiw	GET_LONG(s, strtabsize);
356260684Skaiw
357260684Skaiw	s0 = s;			/* Start of string table. */
358276371Semaste	if (strtabsize < 0 || s0 + strtabsize > end)
359260684Skaiw		goto symtaberror;
360260684Skaiw
361276371Semaste	nentries = (size_t) arraysize / entrysize;
362260684Skaiw
363260684Skaiw	/*
364260684Skaiw	 * Allocate space for the returned Elf_Arsym array.
365260684Skaiw	 */
366260684Skaiw	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
367260684Skaiw		LIBELF_SET_ERROR(RESOURCE, 0);
368260684Skaiw		return (NULL);
369260684Skaiw	}
370260684Skaiw
371260684Skaiw	/* Read in symbol table entries. */
372260684Skaiw	for (n = 0, sym = symtab; n < nentries; n++, sym++) {
373260684Skaiw		GET_LONG(p, stroffset);
374260684Skaiw		GET_LONG(p, fileoffset);
375260684Skaiw
376276371Semaste		if (stroffset < 0 || fileoffset <  0 ||
377367466Sdim		    (off_t) fileoffset >= e->e_rawsize)
378276371Semaste			goto symtaberror;
379276371Semaste
380260684Skaiw		s = s0 + stroffset;
381260684Skaiw
382260684Skaiw		if (s >= end)
383260684Skaiw			goto symtaberror;
384260684Skaiw
385276371Semaste		sym->as_off = (off_t) fileoffset;
386260684Skaiw		sym->as_hash = elf_hash((char *) s);
387260684Skaiw		sym->as_name = (char *) s;
388260684Skaiw	}
389260684Skaiw
390260684Skaiw	/* Fill up the sentinel entry. */
391260684Skaiw	sym->as_name = NULL;
392260684Skaiw	sym->as_hash = ~0UL;
393260684Skaiw	sym->as_off = (off_t) 0;
394260684Skaiw
395260684Skaiw	/* Remember the processed symbol table. */
396260684Skaiw	e->e_u.e_ar.e_symtab = symtab;
397260684Skaiw
398260684Skaiw	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
399260684Skaiw
400260684Skaiw	return (symtab);
401260684Skaiw
402260684Skaiwsymtaberror:
403260684Skaiw	if (symtab)
404260684Skaiw		free(symtab);
405260684Skaiw	LIBELF_SET_ERROR(ARCHIVE, 0);
406260684Skaiw	return (NULL);
407260684Skaiw}
408260684Skaiw
409260684Skaiw/*
410260684Skaiw * An SVR4-style ar(1) symbol table has the following layout:
411260684Skaiw *
412260684Skaiw * - The first 4 bytes are a binary count of the number of entries in the
413260684Skaiw *   symbol table, stored MSB-first.
414260684Skaiw * - Then there are 'n' 4-byte binary offsets, also stored MSB first.
415260684Skaiw * - Following this, there are 'n' null-terminated strings.
416260684Skaiw */
417260684Skaiw
418260684Skaiw#define	GET_WORD(P, V) do {			\
419260684Skaiw		(V) = 0;			\
420260684Skaiw		(V) = (P)[0]; (V) <<= 8;	\
421260684Skaiw		(V) += (P)[1]; (V) <<= 8;	\
422260684Skaiw		(V) += (P)[2]; (V) <<= 8;	\
423260684Skaiw		(V) += (P)[3];			\
424260684Skaiw	} while (0)
425260684Skaiw
426260684Skaiw#define	INTSZ	4
427260684Skaiw
428260684Skaiw
429260684SkaiwElf_Arsym *
430260684Skaiw_libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
431260684Skaiw{
432276371Semaste	uint32_t off;
433276371Semaste	size_t n, nentries;
434260684Skaiw	Elf_Arsym *symtab, *sym;
435260684Skaiw	unsigned char *p, *s, *end;
436260684Skaiw
437260684Skaiw	assert(e != NULL);
438260684Skaiw	assert(count != NULL);
439260684Skaiw	assert(e->e_u.e_ar.e_symtab == NULL);
440260684Skaiw
441260684Skaiw	symtab = NULL;
442260684Skaiw
443260684Skaiw	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
444260684Skaiw		goto symtaberror;
445260684Skaiw
446260684Skaiw	p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
447260684Skaiw	end = p + e->e_u.e_ar.e_rawsymtabsz;
448260684Skaiw
449260684Skaiw	GET_WORD(p, nentries);
450260684Skaiw	p += INTSZ;
451260684Skaiw
452260684Skaiw	if (nentries == 0 || p + nentries * INTSZ >= end)
453260684Skaiw		goto symtaberror;
454260684Skaiw
455260684Skaiw	/* Allocate space for a nentries + a sentinel. */
456260684Skaiw	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
457260684Skaiw		LIBELF_SET_ERROR(RESOURCE, 0);
458260684Skaiw		return (NULL);
459260684Skaiw	}
460260684Skaiw
461260684Skaiw	s = p + (nentries * INTSZ); /* start of the string table. */
462260684Skaiw
463260684Skaiw	for (n = nentries, sym = symtab; n > 0; n--) {
464260684Skaiw		if (s >= end)
465260684Skaiw			goto symtaberror;
466260684Skaiw
467260684Skaiw		GET_WORD(p, off);
468276371Semaste		if (off >= e->e_rawsize)
469276371Semaste			goto symtaberror;
470260684Skaiw
471276371Semaste		sym->as_off = (off_t) off;
472260684Skaiw		sym->as_hash = elf_hash((char *) s);
473260684Skaiw		sym->as_name = (char *) s;
474260684Skaiw
475260684Skaiw		p += INTSZ;
476260684Skaiw		sym++;
477260684Skaiw
478260684Skaiw		for (; s < end && *s++ != '\0';) /* skip to next string */
479260684Skaiw			;
480260684Skaiw	}
481260684Skaiw
482260684Skaiw	/* Fill up the sentinel entry. */
483260684Skaiw	sym->as_name = NULL;
484260684Skaiw	sym->as_hash = ~0UL;
485260684Skaiw	sym->as_off = (off_t) 0;
486260684Skaiw
487260684Skaiw	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
488260684Skaiw	e->e_u.e_ar.e_symtab = symtab;
489260684Skaiw
490260684Skaiw	return (symtab);
491260684Skaiw
492260684Skaiwsymtaberror:
493260684Skaiw	if (symtab)
494260684Skaiw		free(symtab);
495260684Skaiw	LIBELF_SET_ERROR(ARCHIVE, 0);
496260684Skaiw	return (NULL);
497260684Skaiw}
498