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