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