libelf_ar.c revision 1.3
193139Sru/*	$NetBSD: libelf_ar.c,v 1.3 2016/02/20 02:43:42 christos Exp $	*/
2146515Sru
393139Sru/*-
4146515Sru * Copyright (c) 2006,2008,2010 Joseph Koshy
593139Sru * All rights reserved.
693139Sru *
793139Sru * Redistribution and use in source and binary forms, with or without
893139Sru * modification, are permitted provided that the following conditions
993139Sru * are met:
1093139Sru * 1. Redistributions of source code must retain the above copyright
1193139Sru *    notice, this list of conditions and the following disclaimer.
1293139Sru * 2. Redistributions in binary form must reproduce the above copyright
1393139Sru *    notice, this list of conditions and the following disclaimer in the
1493139Sru *    documentation and/or other materials provided with the distribution.
1593139Sru *
1693139Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
1793139Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1893139Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1993139Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20146515Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2193139Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2293139Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2393139Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2493139Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2593139Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2693139Sru * SUCH DAMAGE.
2793139Sru */
2893139Sru
2993139Sru#if HAVE_NBTOOL_CONFIG_H
3093139Sru# include "nbtool_config.h"
31146515Sru#endif
32146515Sru
3393139Sru#include <sys/cdefs.h>
3493139Sru
3593139Sru#include <assert.h>
3693139Sru#include <ctype.h>
3793139Sru#include <libelf.h>
38114472Sru#include <stdlib.h>
39114472Sru#include <string.h>
40114472Sru
41114472Sru#include "_libelf.h"
42114472Sru#include "_libelf_ar.h"
43146515Sru
44146515Sru__RCSID("$NetBSD: libelf_ar.c,v 1.3 2016/02/20 02:43:42 christos Exp $");
45146515SruELFTC_VCSID("Id: libelf_ar.c 3174 2015-03-27 17:13:41Z emaste ");
46146515Sru
47146515Sru#define	LIBELF_NALLOC_SIZE	16
48146515Sru
49146515Sru/*
5093139Sru * `ar' archive handling.
5193139Sru *
52146515Sru * `ar' archives start with signature `ARMAG'.  Each archive member is
5393139Sru * preceded by a header containing meta-data for the member.  This
54146515Sru * header is described in <ar.h> (struct ar_hdr).  The header always
5593139Sru * starts on an even address.  File data is padded with "\n"
56146515Sru * characters to keep this invariant.
57146515Sru *
58100513Sru * Special considerations for `ar' archives:
59146515Sru *
60146515Sru * There are two variants of the `ar' archive format: traditional BSD
61146515Sru * and SVR4.  These differ in the way long file names are treated, and
62146515Sru * in the layout of the archive symbol table.
6393139Sru *
64146515Sru * The `ar' header only has space for a 16 character file name.
65146515Sru *
6693139Sru * In the SVR4 format, file names are terminated with a '/', so this
67146515Sru * effectively leaves 15 characters for the actual file name.  Longer
68146515Sru * file names stored in a separate 'string table' and referenced
69146515Sru * indirectly from the name field.  The string table itself appears as
70146515Sru * an archive member with name "// ".  An `indirect' file name in an
71146515Sru * `ar' header matches the pattern "/[0-9]*". The digits form a
7293139Sru * decimal number that corresponds to a byte offset into the string
7393139Sru * table where the actual file name of the object starts.  Strings in
7493139Sru * the string table are padded to start on even addresses.
7593139Sru *
76100513Sru * In the BSD format, file names can be upto 16 characters.  File
77146515Sru * names shorter than 16 characters are padded to 16 characters using
78146515Sru * (ASCII) space characters.  File names with embedded spaces and file
79146515Sru * names longer than 16 characters are stored immediately after the
8093139Sru * archive header and the name field set to a special indirect name
8193139Sru * matching the pattern "#1/[0-9]+".  The digits form a decimal number
82146515Sru * that corresponds to the actual length of the file name following
83100513Sru * the archive header.  The content of the archive member immediately
84100513Sru * follows the file name, and the size field of the archive member
8593139Sru * holds the sum of the sizes of the member and of the appended file
8693139Sru * name.
8793139Sru *
88146515Sru * Archives may also have a symbol table (see ranlib(1)), mapping
89146515Sru * program symbols to object files inside the archive.
90146515Sru *
9193139Sru * In the SVR4 format, a symbol table uses a file name of "/ " in its
92146515Sru * archive header.  The symbol table is structured as:
93100513Sru *  - a 4-byte count of entries stored as a binary value, MSB first
94100513Sru *  - 'n' 4-byte offsets, stored as binary values, MSB first
9593139Sru *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
96146515Sru *
97114472Sru * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
98146515Sru * It is structured as two parts:
99146515Sru *  - The first part is an array of "ranlib" structures preceded by
100146515Sru *    the size of the array in bytes.  Each "ranlib" structure
101146515Sru *    describes one symbol.  Each structure contains an offset into
102146515Sru *    the string table for the symbol name, and a file offset into the
10393139Sru *    archive for the member defining the symbol.
10493139Sru *  - The second part is a string table containing NUL-terminated
10593139Sru *    strings, preceded by the size of the string table in bytes.
106146515Sru *
107146515Sru * If the symbol table and string table are is present in an archive
108146515Sru * they must be the very first objects and in that order.
109146515Sru */
110146515Sru
111146515Sru
112146515Sru/*
113146515Sru * Retrieve an archive header descriptor.
114146515Sru */
115146515Sru
116146515SruElf_Arhdr *
117146515Sru_libelf_ar_gethdr(Elf *e)
118146515Sru{
119146515Sru	Elf *parent;
120146515Sru	Elf_Arhdr *eh;
121146515Sru	char *namelen;
122146515Sru	size_t n, nlen;
123146515Sru	struct ar_hdr *arh;
124146515Sru
125146515Sru	if ((parent = e->e_parent) == NULL) {
126146515Sru		LIBELF_SET_ERROR(ARGUMENT, 0);
127146515Sru		return (NULL);
128146515Sru	}
129146515Sru
130146515Sru	assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
131146515Sru
132146515Sru	arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
133146515Sru
134146515Sru	assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
135146515Sru	assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile +
136146515Sru	    parent->e_rawsize - sizeof(struct ar_hdr));
137146515Sru
138146515Sru	if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
139146515Sru		LIBELF_SET_ERROR(RESOURCE, 0);
14093139Sru		return (NULL);
141146515Sru	}
142146515Sru
143146515Sru	e->e_hdr.e_arhdr = eh;
144146515Sru	e->e_flags |= LIBELF_F_AR_HEADER;
145146515Sru
146146515Sru	eh->ar_name = eh->ar_rawname = NULL;
147146515Sru
148146515Sru	if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
149146515Sru	    NULL)
150146515Sru		goto error;
151146515Sru
152146515Sru	if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
153146515Sru	    &n) == 0)
154146515Sru		goto error;
155146515Sru	eh->ar_uid = (uid_t) n;
156146515Sru
157146515Sru	if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
158146515Sru	    &n) == 0)
15993139Sru		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 = (unsigned 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	off_t next;
212	size_t nsz, sz;
213	struct ar_hdr *arh;
214	char *member, *namelen;
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(member, sz)) == NULL)
260		return (NULL);
261
262	e->e_fd = fd;
263	e->e_cmd = c;
264	e->e_hdr.e_rawhdr = (unsigned 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.
285 *
286 * We use memcpy() since the source pointer may be misaligned with
287 * respect to the natural alignment for a C 'long'.
288 */
289#define	GET_LONG(P, V)do {				\
290		memcpy(&(V), (P), sizeof(long));	\
291		(P) += sizeof(long);			\
292	} while (/*CONSTCOND*/0)
293
294Elf_Arsym *
295_libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
296{
297	Elf_Arsym *symtab, *sym;
298	unsigned int n, nentries;
299	unsigned char *end, *p, *p0, *s, *s0;
300	const size_t entrysize = 2 * sizeof(long);
301	long arraysize, fileoffset, stroffset, strtabsize;
302
303	assert(e != NULL);
304	assert(count != NULL);
305	assert(e->e_u.e_ar.e_symtab == NULL);
306
307	symtab = NULL;
308
309	/*
310	 * The BSD symbol table always contains the count fields even
311	 * if there are no entries in it.
312	 */
313	if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
314		goto symtaberror;
315
316	p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
317	end = p0 + e->e_u.e_ar.e_rawsymtabsz;
318
319	/*
320	 * Retrieve the size of the array of ranlib descriptors and
321	 * check it for validity.
322	 */
323	GET_LONG(p, arraysize);
324
325	if (arraysize < 0 || p0 + arraysize >= end ||
326	    ((size_t) arraysize % entrysize != 0))
327		goto symtaberror;
328
329	/*
330	 * Check the value of the string table size.
331	 */
332	s = p + arraysize;
333	GET_LONG(s, strtabsize);
334
335	s0 = s;			/* Start of string table. */
336	if (strtabsize < 0 || s0 + strtabsize > end)
337		goto symtaberror;
338
339	nentries = (size_t) arraysize / entrysize;
340
341	/*
342	 * Allocate space for the returned Elf_Arsym array.
343	 */
344	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
345		LIBELF_SET_ERROR(RESOURCE, 0);
346		return (NULL);
347	}
348
349	/* Read in symbol table entries. */
350	for (n = 0, sym = symtab; n < nentries; n++, sym++) {
351		GET_LONG(p, stroffset);
352		GET_LONG(p, fileoffset);
353
354		if (stroffset < 0 || fileoffset <  0 ||
355		    (size_t) fileoffset >= e->e_rawsize)
356			goto symtaberror;
357
358		s = s0 + stroffset;
359
360		if (s >= end)
361			goto symtaberror;
362
363		sym->as_off = (off_t) fileoffset;
364		sym->as_hash = elf_hash((char *) s);
365		sym->as_name = (char *) s;
366	}
367
368	/* Fill up the sentinel entry. */
369	sym->as_name = NULL;
370	sym->as_hash = ~0UL;
371	sym->as_off = (off_t) 0;
372
373	/* Remember the processed symbol table. */
374	e->e_u.e_ar.e_symtab = symtab;
375
376	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
377
378	return (symtab);
379
380symtaberror:
381	if (symtab)
382		free(symtab);
383	LIBELF_SET_ERROR(ARCHIVE, 0);
384	return (NULL);
385}
386
387/*
388 * An SVR4-style ar(1) symbol table has the following layout:
389 *
390 * - The first 4 bytes are a binary count of the number of entries in the
391 *   symbol table, stored MSB-first.
392 * - Then there are 'n' 4-byte binary offsets, also stored MSB first.
393 * - Following this, there are 'n' null-terminated strings.
394 */
395
396#define	GET_WORD(P, V) do {			\
397		(V) = 0;			\
398		(V) = (P)[0]; (V) <<= 8;	\
399		(V) += (P)[1]; (V) <<= 8;	\
400		(V) += (P)[2]; (V) <<= 8;	\
401		(V) += (P)[3];			\
402	} while (0)
403
404#define	INTSZ	4
405
406
407Elf_Arsym *
408_libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
409{
410	uint32_t off;
411	size_t n, nentries;
412	Elf_Arsym *symtab, *sym;
413	unsigned char *p, *s, *end;
414
415	assert(e != NULL);
416	assert(count != NULL);
417	assert(e->e_u.e_ar.e_symtab == NULL);
418
419	symtab = NULL;
420
421	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
422		goto symtaberror;
423
424	p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
425	end = p + e->e_u.e_ar.e_rawsymtabsz;
426
427	GET_WORD(p, nentries);
428	p += INTSZ;
429
430	if (nentries == 0 || p + nentries * INTSZ >= end)
431		goto symtaberror;
432
433	/* Allocate space for a nentries + a sentinel. */
434	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
435		LIBELF_SET_ERROR(RESOURCE, 0);
436		return (NULL);
437	}
438
439	s = p + (nentries * INTSZ); /* start of the string table. */
440
441	for (n = nentries, sym = symtab; n > 0; n--) {
442		if (s >= end)
443			goto symtaberror;
444
445		GET_WORD(p, off);
446		if (off >= e->e_rawsize)
447			goto symtaberror;
448
449		sym->as_off = (off_t) off;
450		sym->as_hash = elf_hash((char *) s);
451		sym->as_name = (char *) s;
452
453		p += INTSZ;
454		sym++;
455
456		for (; s < end && *s++ != '\0';) /* skip to next string */
457			;
458	}
459
460	/* Fill up the sentinel entry. */
461	sym->as_name = NULL;
462	sym->as_hash = ~0UL;
463	sym->as_off = (off_t) 0;
464
465	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
466	e->e_u.e_ar.e_symtab = symtab;
467
468	return (symtab);
469
470symtaberror:
471	if (symtab)
472		free(symtab);
473	LIBELF_SET_ERROR(ARCHIVE, 0);
474	return (NULL);
475}
476