xref: /openbsd/lib/libelf/libelf_ar.c (revision e219834f)
1a1b5ec25Sjsg /*-
2a1b5ec25Sjsg  * Copyright (c) 2006,2008,2010 Joseph Koshy
3a1b5ec25Sjsg  * All rights reserved.
4a1b5ec25Sjsg  *
5a1b5ec25Sjsg  * Redistribution and use in source and binary forms, with or without
6a1b5ec25Sjsg  * modification, are permitted provided that the following conditions
7a1b5ec25Sjsg  * are met:
8a1b5ec25Sjsg  * 1. Redistributions of source code must retain the above copyright
9a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer.
10a1b5ec25Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
11a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer in the
12a1b5ec25Sjsg  *    documentation and/or other materials provided with the distribution.
13a1b5ec25Sjsg  *
14a1b5ec25Sjsg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
15a1b5ec25Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1b5ec25Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1b5ec25Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1b5ec25Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1b5ec25Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1b5ec25Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1b5ec25Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1b5ec25Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1b5ec25Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1b5ec25Sjsg  * SUCH DAMAGE.
25a1b5ec25Sjsg  */
26a1b5ec25Sjsg 
27a1b5ec25Sjsg #include <assert.h>
28a1b5ec25Sjsg #include <ctype.h>
29a1b5ec25Sjsg #include <libelf.h>
30a1b5ec25Sjsg #include <stdlib.h>
31a1b5ec25Sjsg #include <string.h>
32a1b5ec25Sjsg #include <stdint.h>
33a1b5ec25Sjsg 
34a1b5ec25Sjsg #include "_libelf.h"
35a1b5ec25Sjsg #include "_libelf_ar.h"
36a1b5ec25Sjsg 
37*e219834fSjsg ELFTC_VCSID("$Id: libelf_ar.c,v 1.2 2019/03/19 02:31:35 jsg Exp $");
38a1b5ec25Sjsg 
39a1b5ec25Sjsg #define	LIBELF_NALLOC_SIZE	16
40a1b5ec25Sjsg 
41a1b5ec25Sjsg /*
42a1b5ec25Sjsg  * `ar' archive handling.
43a1b5ec25Sjsg  *
44a1b5ec25Sjsg  * `ar' archives start with signature `ARMAG'.  Each archive member is
45a1b5ec25Sjsg  * preceded by a header containing meta-data for the member.  This
46a1b5ec25Sjsg  * header is described in <ar.h> (struct ar_hdr).  The header always
47a1b5ec25Sjsg  * starts on an even address.  File data is padded with "\n"
48a1b5ec25Sjsg  * characters to keep this invariant.
49a1b5ec25Sjsg  *
50a1b5ec25Sjsg  * Special considerations for `ar' archives:
51a1b5ec25Sjsg  *
52a1b5ec25Sjsg  * There are two variants of the `ar' archive format: traditional BSD
53a1b5ec25Sjsg  * and SVR4.  These differ in the way long file names are treated, and
54a1b5ec25Sjsg  * in the layout of the archive symbol table.
55a1b5ec25Sjsg  *
56a1b5ec25Sjsg  * The `ar' header only has space for a 16 character file name.
57a1b5ec25Sjsg  *
58a1b5ec25Sjsg  * In the SVR4 format, file names are terminated with a '/', so this
59a1b5ec25Sjsg  * effectively leaves 15 characters for the actual file name.  Longer
60a1b5ec25Sjsg  * file names stored in a separate 'string table' and referenced
61a1b5ec25Sjsg  * indirectly from the name field.  The string table itself appears as
62a1b5ec25Sjsg  * an archive member with name "// ".  An `indirect' file name in an
63a1b5ec25Sjsg  * `ar' header matches the pattern "/[0-9]*". The digits form a
64a1b5ec25Sjsg  * decimal number that corresponds to a byte offset into the string
65a1b5ec25Sjsg  * table where the actual file name of the object starts.  Strings in
66a1b5ec25Sjsg  * the string table are padded to start on even addresses.
67a1b5ec25Sjsg  *
68a1b5ec25Sjsg  * In the BSD format, file names can be up to 16 characters.  File
69a1b5ec25Sjsg  * names shorter than 16 characters are padded to 16 characters using
70a1b5ec25Sjsg  * (ASCII) space characters.  File names with embedded spaces and file
71a1b5ec25Sjsg  * names longer than 16 characters are stored immediately after the
72a1b5ec25Sjsg  * archive header and the name field set to a special indirect name
73a1b5ec25Sjsg  * matching the pattern "#1/[0-9]+".  The digits form a decimal number
74a1b5ec25Sjsg  * that corresponds to the actual length of the file name following
75a1b5ec25Sjsg  * the archive header.  The content of the archive member immediately
76a1b5ec25Sjsg  * follows the file name, and the size field of the archive member
77a1b5ec25Sjsg  * holds the sum of the sizes of the member and of the appended file
78a1b5ec25Sjsg  * name.
79a1b5ec25Sjsg  *
80a1b5ec25Sjsg  * Archives may also have a symbol table (see ranlib(1)), mapping
81a1b5ec25Sjsg  * program symbols to object files inside the archive.
82a1b5ec25Sjsg  *
83a1b5ec25Sjsg  * In the SVR4 format, a symbol table uses a file name of "/ " in its
84a1b5ec25Sjsg  * archive header.  The symbol table is structured as:
85a1b5ec25Sjsg  *  - a 4-byte count of entries stored as a binary value, MSB first
86a1b5ec25Sjsg  *  - 'n' 4-byte offsets, stored as binary values, MSB first
87a1b5ec25Sjsg  *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
88a1b5ec25Sjsg  *
89a1b5ec25Sjsg  * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
90a1b5ec25Sjsg  * It is structured as two parts:
91a1b5ec25Sjsg  *  - The first part is an array of "ranlib" structures preceded by
92a1b5ec25Sjsg  *    the size of the array in bytes.  Each "ranlib" structure
93a1b5ec25Sjsg  *    describes one symbol.  Each structure contains an offset into
94a1b5ec25Sjsg  *    the string table for the symbol name, and a file offset into the
95a1b5ec25Sjsg  *    archive for the member defining the symbol.
96a1b5ec25Sjsg  *  - The second part is a string table containing NUL-terminated
97a1b5ec25Sjsg  *    strings, preceded by the size of the string table in bytes.
98a1b5ec25Sjsg  *
99a1b5ec25Sjsg  * If the symbol table and string table are is present in an archive
100a1b5ec25Sjsg  * they must be the very first objects and in that order.
101a1b5ec25Sjsg  */
102a1b5ec25Sjsg 
103a1b5ec25Sjsg 
104a1b5ec25Sjsg /*
105a1b5ec25Sjsg  * Retrieve an archive header descriptor.
106a1b5ec25Sjsg  */
107a1b5ec25Sjsg 
108a1b5ec25Sjsg Elf_Arhdr *
_libelf_ar_gethdr(Elf * e)109a1b5ec25Sjsg _libelf_ar_gethdr(Elf *e)
110a1b5ec25Sjsg {
111a1b5ec25Sjsg 	Elf *parent;
112a1b5ec25Sjsg 	Elf_Arhdr *eh;
113a1b5ec25Sjsg 	char *namelen;
114a1b5ec25Sjsg 	size_t n, nlen;
115a1b5ec25Sjsg 	struct ar_hdr *arh;
116a1b5ec25Sjsg 
117a1b5ec25Sjsg 	if ((parent = e->e_parent) == NULL) {
118a1b5ec25Sjsg 		LIBELF_SET_ERROR(ARGUMENT, 0);
119a1b5ec25Sjsg 		return (NULL);
120a1b5ec25Sjsg 	}
121a1b5ec25Sjsg 
122a1b5ec25Sjsg 	assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
123a1b5ec25Sjsg 
124a1b5ec25Sjsg 	arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
125a1b5ec25Sjsg 
126a1b5ec25Sjsg 	assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
127*e219834fSjsg 
128*e219834fSjsg 	/*
129*e219834fSjsg 	 * There needs to be enough space remaining in the file for the
130*e219834fSjsg 	 * archive header.
131*e219834fSjsg 	 */
132*e219834fSjsg 	if ((uintptr_t) arh > (uintptr_t) parent->e_rawfile +
133*e219834fSjsg 	    (uintptr_t) parent->e_rawsize - sizeof(struct ar_hdr)) {
134*e219834fSjsg 		LIBELF_SET_ERROR(ARCHIVE, 0);
135*e219834fSjsg 		return (NULL);
136*e219834fSjsg 	}
137a1b5ec25Sjsg 
138a1b5ec25Sjsg 	if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
139a1b5ec25Sjsg 		LIBELF_SET_ERROR(RESOURCE, 0);
140a1b5ec25Sjsg 		return (NULL);
141a1b5ec25Sjsg 	}
142a1b5ec25Sjsg 
143a1b5ec25Sjsg 	e->e_hdr.e_arhdr = eh;
144a1b5ec25Sjsg 	e->e_flags |= LIBELF_F_AR_HEADER;
145a1b5ec25Sjsg 
146a1b5ec25Sjsg 	eh->ar_name = eh->ar_rawname = NULL;
147a1b5ec25Sjsg 
148a1b5ec25Sjsg 	if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
149a1b5ec25Sjsg 	    NULL)
150a1b5ec25Sjsg 		goto error;
151a1b5ec25Sjsg 
152a1b5ec25Sjsg 	if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
153a1b5ec25Sjsg 	    &n) == 0)
154a1b5ec25Sjsg 		goto error;
155a1b5ec25Sjsg 	eh->ar_uid = (uid_t) n;
156a1b5ec25Sjsg 
157a1b5ec25Sjsg 	if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
158a1b5ec25Sjsg 	    &n) == 0)
159a1b5ec25Sjsg 		goto error;
160a1b5ec25Sjsg 	eh->ar_gid = (gid_t) n;
161a1b5ec25Sjsg 
162a1b5ec25Sjsg 	if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8,
163a1b5ec25Sjsg 	    &n) == 0)
164a1b5ec25Sjsg 		goto error;
165a1b5ec25Sjsg 	eh->ar_mode = (mode_t) n;
166a1b5ec25Sjsg 
167a1b5ec25Sjsg 	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
168a1b5ec25Sjsg 	    &n) == 0)
169a1b5ec25Sjsg 		goto error;
170a1b5ec25Sjsg 
171a1b5ec25Sjsg 	/*
172a1b5ec25Sjsg 	 * Get the true size of the member if extended naming is being used.
173a1b5ec25Sjsg 	 */
174a1b5ec25Sjsg 	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
175a1b5ec25Sjsg 		namelen = arh->ar_name +
176a1b5ec25Sjsg 		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
177a1b5ec25Sjsg 		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
178a1b5ec25Sjsg 		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
179a1b5ec25Sjsg 			goto error;
180a1b5ec25Sjsg 		n -= nlen;
181a1b5ec25Sjsg 	}
182a1b5ec25Sjsg 
183a1b5ec25Sjsg 	eh->ar_size = n;
184a1b5ec25Sjsg 
185a1b5ec25Sjsg 	if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
186a1b5ec25Sjsg 		goto error;
187a1b5ec25Sjsg 
188a1b5ec25Sjsg 	eh->ar_flags = 0;
189a1b5ec25Sjsg 
190a1b5ec25Sjsg 	return (eh);
191a1b5ec25Sjsg 
192a1b5ec25Sjsg  error:
193a1b5ec25Sjsg 	if (eh) {
194a1b5ec25Sjsg 		if (eh->ar_name)
195a1b5ec25Sjsg 			free(eh->ar_name);
196a1b5ec25Sjsg 		if (eh->ar_rawname)
197a1b5ec25Sjsg 			free(eh->ar_rawname);
198a1b5ec25Sjsg 		free(eh);
199a1b5ec25Sjsg 	}
200a1b5ec25Sjsg 
201a1b5ec25Sjsg 	e->e_flags &= ~LIBELF_F_AR_HEADER;
202a1b5ec25Sjsg 	e->e_hdr.e_rawhdr = (unsigned char *) arh;
203a1b5ec25Sjsg 
204a1b5ec25Sjsg 	return (NULL);
205a1b5ec25Sjsg }
206a1b5ec25Sjsg 
207a1b5ec25Sjsg Elf *
_libelf_ar_open_member(int fd,Elf_Cmd c,Elf * elf)208a1b5ec25Sjsg _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
209a1b5ec25Sjsg {
210a1b5ec25Sjsg 	Elf *e;
211a1b5ec25Sjsg 	size_t nsz, sz;
212*e219834fSjsg 	off_t next, end;
213a1b5ec25Sjsg 	struct ar_hdr *arh;
214a1b5ec25Sjsg 	char *member, *namelen;
215a1b5ec25Sjsg 
216a1b5ec25Sjsg 	assert(elf->e_kind == ELF_K_AR);
217a1b5ec25Sjsg 
218a1b5ec25Sjsg 	next = elf->e_u.e_ar.e_next;
219a1b5ec25Sjsg 
220a1b5ec25Sjsg 	/*
221a1b5ec25Sjsg 	 * `next' is only set to zero by elf_next() when the last
222a1b5ec25Sjsg 	 * member of an archive is processed.
223a1b5ec25Sjsg 	 */
224a1b5ec25Sjsg 	if (next == (off_t) 0)
225a1b5ec25Sjsg 		return (NULL);
226a1b5ec25Sjsg 
227a1b5ec25Sjsg 	assert((next & 1) == 0);
228a1b5ec25Sjsg 
229*e219834fSjsg 	/*
230*e219834fSjsg 	 * There needs to be enough space in the file to contain an
231*e219834fSjsg 	 * ar(1) header.
232*e219834fSjsg 	 */
233*e219834fSjsg 	end = next + (off_t) sizeof(struct ar_hdr);
234*e219834fSjsg 	if ((uintmax_t) end < (uintmax_t) next || /* Overflow. */
235*e219834fSjsg 	    end > (off_t) elf->e_rawsize) {
236*e219834fSjsg 		LIBELF_SET_ERROR(ARCHIVE, 0);
237*e219834fSjsg 		return (NULL);
238*e219834fSjsg 	}
239*e219834fSjsg 
240a1b5ec25Sjsg 	arh = (struct ar_hdr *) (elf->e_rawfile + next);
241a1b5ec25Sjsg 
242a1b5ec25Sjsg 	/*
243a1b5ec25Sjsg 	 * Retrieve the size of the member.
244a1b5ec25Sjsg 	 */
245a1b5ec25Sjsg 	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
246a1b5ec25Sjsg 	    &sz) == 0) {
247a1b5ec25Sjsg 		LIBELF_SET_ERROR(ARCHIVE, 0);
248a1b5ec25Sjsg 		return (NULL);
249a1b5ec25Sjsg 	}
250a1b5ec25Sjsg 
251a1b5ec25Sjsg 	/*
252*e219834fSjsg 	 * Check if the archive member that follows will fit in the
253*e219834fSjsg 	 * containing archive.
254*e219834fSjsg 	 */
255*e219834fSjsg 	end += (off_t) sz;
256*e219834fSjsg 	if (end < next || /* Overflow. */
257*e219834fSjsg 	    end > (off_t) elf->e_rawsize) {
258*e219834fSjsg 		LIBELF_SET_ERROR(ARCHIVE, 0);
259*e219834fSjsg 		return (NULL);
260*e219834fSjsg 	}
261*e219834fSjsg 
262*e219834fSjsg 	/*
263a1b5ec25Sjsg 	 * Adjust the size field for members in BSD archives using
264a1b5ec25Sjsg 	 * extended naming.
265a1b5ec25Sjsg 	 */
266a1b5ec25Sjsg 	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
267a1b5ec25Sjsg 		namelen = arh->ar_name +
268a1b5ec25Sjsg 		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
269a1b5ec25Sjsg 		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
270a1b5ec25Sjsg 		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
271a1b5ec25Sjsg 			LIBELF_SET_ERROR(ARCHIVE, 0);
272a1b5ec25Sjsg 			return (NULL);
273a1b5ec25Sjsg 		}
274a1b5ec25Sjsg 
275a1b5ec25Sjsg 		member = (char *) (arh + 1) + nsz;
276a1b5ec25Sjsg 		sz -= nsz;
277a1b5ec25Sjsg 	} else
278a1b5ec25Sjsg 		member = (char *) (arh + 1);
279a1b5ec25Sjsg 
280a1b5ec25Sjsg 
281a1b5ec25Sjsg 	if ((e = elf_memory(member, sz)) == NULL)
282a1b5ec25Sjsg 		return (NULL);
283a1b5ec25Sjsg 
284a1b5ec25Sjsg 	e->e_fd = fd;
285a1b5ec25Sjsg 	e->e_cmd = c;
286a1b5ec25Sjsg 	e->e_hdr.e_rawhdr = (unsigned char *) arh;
287a1b5ec25Sjsg 
288a1b5ec25Sjsg 	elf->e_u.e_ar.e_nchildren++;
289a1b5ec25Sjsg 	e->e_parent = elf;
290a1b5ec25Sjsg 
291a1b5ec25Sjsg 	return (e);
292a1b5ec25Sjsg }
293a1b5ec25Sjsg 
294a1b5ec25Sjsg /*
295a1b5ec25Sjsg  * A BSD-style ar(1) symbol table has the following layout:
296a1b5ec25Sjsg  *
297a1b5ec25Sjsg  * - A count of bytes used by the following array of 'ranlib'
298a1b5ec25Sjsg  *   structures, stored as a 'long'.
299a1b5ec25Sjsg  * - An array of 'ranlib' structures.  Each array element is
300a1b5ec25Sjsg  *   two 'long's in size.
301a1b5ec25Sjsg  * - A count of bytes used for the following symbol table.
302a1b5ec25Sjsg  * - The symbol table itself.
303a1b5ec25Sjsg  */
304a1b5ec25Sjsg 
305a1b5ec25Sjsg /*
306a1b5ec25Sjsg  * A helper macro to read in a 'long' value from the archive.
307a1b5ec25Sjsg  *
308a1b5ec25Sjsg  * We use memcpy() since the source pointer may be misaligned with
309a1b5ec25Sjsg  * respect to the natural alignment for a C 'long'.
310a1b5ec25Sjsg  */
311a1b5ec25Sjsg #define	GET_LONG(P, V)do {				\
312a1b5ec25Sjsg 		memcpy(&(V), (P), sizeof(long));	\
313a1b5ec25Sjsg 		(P) += sizeof(long);			\
314a1b5ec25Sjsg 	} while (0)
315a1b5ec25Sjsg 
316a1b5ec25Sjsg Elf_Arsym *
_libelf_ar_process_bsd_symtab(Elf * e,size_t * count)317a1b5ec25Sjsg _libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
318a1b5ec25Sjsg {
319a1b5ec25Sjsg 	Elf_Arsym *symtab, *sym;
320*e219834fSjsg 	unsigned int n;
321*e219834fSjsg 	size_t nentries;
322a1b5ec25Sjsg 	unsigned char *end, *p, *p0, *s, *s0;
323a1b5ec25Sjsg 	const size_t entrysize = 2 * sizeof(long);
324a1b5ec25Sjsg 	long arraysize, fileoffset, stroffset, strtabsize;
325a1b5ec25Sjsg 
326a1b5ec25Sjsg 	assert(e != NULL);
327a1b5ec25Sjsg 	assert(count != NULL);
328a1b5ec25Sjsg 	assert(e->e_u.e_ar.e_symtab == NULL);
329a1b5ec25Sjsg 
330a1b5ec25Sjsg 	symtab = NULL;
331a1b5ec25Sjsg 
332a1b5ec25Sjsg 	/*
333a1b5ec25Sjsg 	 * The BSD symbol table always contains the count fields even
334a1b5ec25Sjsg 	 * if there are no entries in it.
335a1b5ec25Sjsg 	 */
336a1b5ec25Sjsg 	if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
337a1b5ec25Sjsg 		goto symtaberror;
338a1b5ec25Sjsg 
339a1b5ec25Sjsg 	p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
340a1b5ec25Sjsg 	end = p0 + e->e_u.e_ar.e_rawsymtabsz;
341a1b5ec25Sjsg 
342a1b5ec25Sjsg 	/*
343a1b5ec25Sjsg 	 * Retrieve the size of the array of ranlib descriptors and
344a1b5ec25Sjsg 	 * check it for validity.
345a1b5ec25Sjsg 	 */
346a1b5ec25Sjsg 	GET_LONG(p, arraysize);
347a1b5ec25Sjsg 
348a1b5ec25Sjsg 	if (arraysize < 0 || p0 + arraysize >= end ||
349a1b5ec25Sjsg 	    ((size_t) arraysize % entrysize != 0))
350a1b5ec25Sjsg 		goto symtaberror;
351a1b5ec25Sjsg 
352a1b5ec25Sjsg 	/*
353a1b5ec25Sjsg 	 * Check the value of the string table size.
354a1b5ec25Sjsg 	 */
355a1b5ec25Sjsg 	s = p + arraysize;
356a1b5ec25Sjsg 	GET_LONG(s, strtabsize);
357a1b5ec25Sjsg 
358a1b5ec25Sjsg 	s0 = s;			/* Start of string table. */
359a1b5ec25Sjsg 	if (strtabsize < 0 || s0 + strtabsize > end)
360a1b5ec25Sjsg 		goto symtaberror;
361a1b5ec25Sjsg 
362a1b5ec25Sjsg 	nentries = (size_t) arraysize / entrysize;
363a1b5ec25Sjsg 
364a1b5ec25Sjsg 	/*
365a1b5ec25Sjsg 	 * Allocate space for the returned Elf_Arsym array.
366a1b5ec25Sjsg 	 */
367a1b5ec25Sjsg 	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
368a1b5ec25Sjsg 		LIBELF_SET_ERROR(RESOURCE, 0);
369a1b5ec25Sjsg 		return (NULL);
370a1b5ec25Sjsg 	}
371a1b5ec25Sjsg 
372a1b5ec25Sjsg 	/* Read in symbol table entries. */
373a1b5ec25Sjsg 	for (n = 0, sym = symtab; n < nentries; n++, sym++) {
374a1b5ec25Sjsg 		GET_LONG(p, stroffset);
375a1b5ec25Sjsg 		GET_LONG(p, fileoffset);
376a1b5ec25Sjsg 
377a1b5ec25Sjsg 		if (stroffset < 0 || fileoffset <  0 ||
378*e219834fSjsg 		    (off_t) fileoffset >= e->e_rawsize)
379a1b5ec25Sjsg 			goto symtaberror;
380a1b5ec25Sjsg 
381a1b5ec25Sjsg 		s = s0 + stroffset;
382a1b5ec25Sjsg 
383a1b5ec25Sjsg 		if (s >= end)
384a1b5ec25Sjsg 			goto symtaberror;
385a1b5ec25Sjsg 
386a1b5ec25Sjsg 		sym->as_off = (off_t) fileoffset;
387a1b5ec25Sjsg 		sym->as_hash = elf_hash((char *) s);
388a1b5ec25Sjsg 		sym->as_name = (char *) s;
389a1b5ec25Sjsg 	}
390a1b5ec25Sjsg 
391a1b5ec25Sjsg 	/* Fill up the sentinel entry. */
392a1b5ec25Sjsg 	sym->as_name = NULL;
393a1b5ec25Sjsg 	sym->as_hash = ~0UL;
394a1b5ec25Sjsg 	sym->as_off = (off_t) 0;
395a1b5ec25Sjsg 
396a1b5ec25Sjsg 	/* Remember the processed symbol table. */
397a1b5ec25Sjsg 	e->e_u.e_ar.e_symtab = symtab;
398a1b5ec25Sjsg 
399a1b5ec25Sjsg 	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
400a1b5ec25Sjsg 
401a1b5ec25Sjsg 	return (symtab);
402a1b5ec25Sjsg 
403a1b5ec25Sjsg symtaberror:
404a1b5ec25Sjsg 	if (symtab)
405a1b5ec25Sjsg 		free(symtab);
406a1b5ec25Sjsg 	LIBELF_SET_ERROR(ARCHIVE, 0);
407a1b5ec25Sjsg 	return (NULL);
408a1b5ec25Sjsg }
409a1b5ec25Sjsg 
410a1b5ec25Sjsg /*
411a1b5ec25Sjsg  * An SVR4-style ar(1) symbol table has the following layout:
412a1b5ec25Sjsg  *
413a1b5ec25Sjsg  * - The first 4 bytes are a binary count of the number of entries in the
414a1b5ec25Sjsg  *   symbol table, stored MSB-first.
415a1b5ec25Sjsg  * - Then there are 'n' 4-byte binary offsets, also stored MSB first.
416a1b5ec25Sjsg  * - Following this, there are 'n' null-terminated strings.
417a1b5ec25Sjsg  */
418a1b5ec25Sjsg 
419a1b5ec25Sjsg #define	GET_WORD(P, V) do {			\
420a1b5ec25Sjsg 		(V) = 0;			\
421a1b5ec25Sjsg 		(V) = (P)[0]; (V) <<= 8;	\
422a1b5ec25Sjsg 		(V) += (P)[1]; (V) <<= 8;	\
423a1b5ec25Sjsg 		(V) += (P)[2]; (V) <<= 8;	\
424a1b5ec25Sjsg 		(V) += (P)[3];			\
425a1b5ec25Sjsg 	} while (0)
426a1b5ec25Sjsg 
427a1b5ec25Sjsg #define	INTSZ	4
428a1b5ec25Sjsg 
429a1b5ec25Sjsg 
430a1b5ec25Sjsg Elf_Arsym *
_libelf_ar_process_svr4_symtab(Elf * e,size_t * count)431a1b5ec25Sjsg _libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
432a1b5ec25Sjsg {
433a1b5ec25Sjsg 	uint32_t off;
434a1b5ec25Sjsg 	size_t n, nentries;
435a1b5ec25Sjsg 	Elf_Arsym *symtab, *sym;
436a1b5ec25Sjsg 	unsigned char *p, *s, *end;
437a1b5ec25Sjsg 
438a1b5ec25Sjsg 	assert(e != NULL);
439a1b5ec25Sjsg 	assert(count != NULL);
440a1b5ec25Sjsg 	assert(e->e_u.e_ar.e_symtab == NULL);
441a1b5ec25Sjsg 
442a1b5ec25Sjsg 	symtab = NULL;
443a1b5ec25Sjsg 
444a1b5ec25Sjsg 	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
445a1b5ec25Sjsg 		goto symtaberror;
446a1b5ec25Sjsg 
447a1b5ec25Sjsg 	p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
448a1b5ec25Sjsg 	end = p + e->e_u.e_ar.e_rawsymtabsz;
449a1b5ec25Sjsg 
450a1b5ec25Sjsg 	GET_WORD(p, nentries);
451a1b5ec25Sjsg 	p += INTSZ;
452a1b5ec25Sjsg 
453a1b5ec25Sjsg 	if (nentries == 0 || p + nentries * INTSZ >= end)
454a1b5ec25Sjsg 		goto symtaberror;
455a1b5ec25Sjsg 
456a1b5ec25Sjsg 	/* Allocate space for a nentries + a sentinel. */
457a1b5ec25Sjsg 	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
458a1b5ec25Sjsg 		LIBELF_SET_ERROR(RESOURCE, 0);
459a1b5ec25Sjsg 		return (NULL);
460a1b5ec25Sjsg 	}
461a1b5ec25Sjsg 
462a1b5ec25Sjsg 	s = p + (nentries * INTSZ); /* start of the string table. */
463a1b5ec25Sjsg 
464a1b5ec25Sjsg 	for (n = nentries, sym = symtab; n > 0; n--) {
465a1b5ec25Sjsg 		if (s >= end)
466a1b5ec25Sjsg 			goto symtaberror;
467a1b5ec25Sjsg 
468a1b5ec25Sjsg 		GET_WORD(p, off);
469a1b5ec25Sjsg 		if (off >= e->e_rawsize)
470a1b5ec25Sjsg 			goto symtaberror;
471a1b5ec25Sjsg 
472a1b5ec25Sjsg 		sym->as_off = (off_t) off;
473a1b5ec25Sjsg 		sym->as_hash = elf_hash((char *) s);
474a1b5ec25Sjsg 		sym->as_name = (char *) s;
475a1b5ec25Sjsg 
476a1b5ec25Sjsg 		p += INTSZ;
477a1b5ec25Sjsg 		sym++;
478a1b5ec25Sjsg 
479a1b5ec25Sjsg 		for (; s < end && *s++ != '\0';) /* skip to next string */
480a1b5ec25Sjsg 			;
481a1b5ec25Sjsg 	}
482a1b5ec25Sjsg 
483a1b5ec25Sjsg 	/* Fill up the sentinel entry. */
484a1b5ec25Sjsg 	sym->as_name = NULL;
485a1b5ec25Sjsg 	sym->as_hash = ~0UL;
486a1b5ec25Sjsg 	sym->as_off = (off_t) 0;
487a1b5ec25Sjsg 
488a1b5ec25Sjsg 	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
489a1b5ec25Sjsg 	e->e_u.e_ar.e_symtab = symtab;
490a1b5ec25Sjsg 
491a1b5ec25Sjsg 	return (symtab);
492a1b5ec25Sjsg 
493a1b5ec25Sjsg symtaberror:
494a1b5ec25Sjsg 	if (symtab)
495a1b5ec25Sjsg 		free(symtab);
496a1b5ec25Sjsg 	LIBELF_SET_ERROR(ARCHIVE, 0);
497a1b5ec25Sjsg 	return (NULL);
498a1b5ec25Sjsg }
499