libelf_ar.c revision 165534
1238104Sdes/*-
2238104Sdes * Copyright (c) 2006 Joseph Koshy
3238104Sdes * All rights reserved.
4238104Sdes *
5238104Sdes * Redistribution and use in source and binary forms, with or without
6238104Sdes * modification, are permitted provided that the following conditions
7238104Sdes * are met:
8238104Sdes * 1. Redistributions of source code must retain the above copyright
9238104Sdes *    notice, this list of conditions and the following disclaimer.
10238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
11238104Sdes *    notice, this list of conditions and the following disclaimer in the
12238104Sdes *    documentation and/or other materials provided with the distribution.
13238104Sdes *
14238104Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
15238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17238104Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18238104Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19238104Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20238104Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24238104Sdes * SUCH DAMAGE.
25238104Sdes */
26238104Sdes
27238104Sdes#include <sys/cdefs.h>
28238104Sdes__FBSDID("$FreeBSD: head/lib/libelf/libelf_ar.c 165534 2006-12-25 02:06:32Z jkoshy $");
29238104Sdes
30238104Sdes#include <ar.h>
31238104Sdes#include <assert.h>
32238104Sdes#include <ctype.h>
33238104Sdes#include <libelf.h>
34238104Sdes#include <stdlib.h>
35238104Sdes#include <string.h>
36238104Sdes
37238104Sdes#include "_libelf.h"
38238104Sdes
39#define	LIBELF_NALLOC_SIZE	16
40
41/*
42 * `ar' archive handling.
43 *
44 * `ar' archives start with signature `ARMAG'.  Each archive member is
45 * preceded by a header containing meta-data for the member.  This
46 * header is described in <ar.h> (struct ar_hdr).  The header always
47 * starts on an even address.  File data is padded with "\n"
48 * characters to keep this invariant.
49 *
50 * Special considerations for `ar' archives:
51 *
52 * The `ar' header only has space for a 16 character file name.  File
53 * names are terminated with a '/', so this effectively leaves 15
54 * characters for the actual file name.  In order to accomodate longer
55 * file names, names may be stored in a separate 'string table' and
56 * referenced indirectly by a member header.  The string table itself
57 * appears as an archive member with name "// ".  An indirect file name
58 * in an `ar' header matches the pattern "/[0-9]*". The digits form a
59 * decimal number that corresponds to a byte offset into the string
60 * table where the actual file name of the object starts.  Strings in
61 * the string table are padded to start on even addresses.
62 *
63 * Archives may also have a symbol table (see ranlib(1)), mapping
64 * program symbols to object files inside the archive.  A symbol table
65 * uses a file name of "/ " in its archive header.  The symbol table
66 * is structured as:
67 *  - a 4-byte count of entries stored as a binary value, MSB first
68 *  - 'n' 4-byte offsets, stored as binary values, MSB first
69 *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
70 *
71 * If the symbol table and string table are is present in an archive
72 * they must be the very first objects and in that order.
73 */
74
75/*
76 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
77 * number in the specified base.
78 */
79static int
80_libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
81{
82	int c, v;
83	size_t r;
84	char *e;
85
86	assert(base <= 10);
87
88	e = s + sz;
89
90	/* skip leading blanks */
91	for (;s < e && (c = *s) == ' '; s++)
92		;
93
94	r = 0L;
95	for (;s < e; s++) {
96		if ((c = *s) == ' ')
97			break;
98		if (c < '0' || c > '9')
99			return (0);
100		v = c - '0';
101		if (v >= base)		/* Illegal digit. */
102			break;
103		r *= base;
104		r += v;
105	}
106
107	*ret = r;
108
109	return (1);
110}
111
112/*
113 * Retrieve a string from a name field.  If `rawname' is set, leave
114 * ar(1) control characters in.
115 */
116static char *
117_libelf_ar_get_string(char *buf, size_t bufsize, int rawname)
118{
119	char *q, *r;
120	size_t sz;
121
122	/* Skip back over trailing blanks. */
123	for (q = buf + bufsize - 1; q > buf && *q == ' '; --q)
124		;
125
126	if (rawname == 0 && *q == '/')
127		q--;
128
129	if (q <= buf)
130		return (NULL);
131
132	sz = q - buf + 2; /* Space for a trailing NUL. */
133	if ((r = malloc(sz)) == NULL) {
134		LIBELF_SET_ERROR(RESOURCE, 0);
135		return (NULL);
136	}
137
138	(void) strncpy(r, buf, sz);
139	r[sz - 1] = '\0';
140
141	return (r);
142}
143
144/*
145 * Retrieve the full name of the archive member.
146 */
147static char *
148_libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
149{
150	char *q, *r, *s;
151	size_t len;
152	size_t offset;
153
154	assert(e->e_kind == ELF_K_AR);
155
156	if (buf[0] == '/') {
157		/*
158		 * The value in field ar_name is a decimal offset into
159		 * the archive string table where the actual name
160		 * resides.
161		 */
162		if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
163		    &offset) == 0) {
164			LIBELF_SET_ERROR(ARCHIVE, 0);
165			return (NULL);
166		}
167
168		if (offset > e->e_u.e_ar.e_rawstrtabsz) {
169			LIBELF_SET_ERROR(ARCHIVE, 0);
170			return (NULL);
171		}
172
173		s = q = e->e_u.e_ar.e_rawstrtab + offset;
174		r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;
175
176		for (s = q; s < r && *s != '/'; s++)
177			;
178		len = s - q + 1; /* space for the trailing NUL */
179
180		if ((s = malloc(len)) == NULL) {
181			LIBELF_SET_ERROR(RESOURCE, 0);
182			return (NULL);
183		}
184
185		(void) strncpy(s, q, len);
186		s[len - 1] = '\0';
187
188		return (s);
189	}
190
191	/*
192	 * Normal 'name'
193	 */
194	return (_libelf_ar_get_string(buf, bufsize, 0));
195}
196
197
198Elf_Arhdr *
199_libelf_ar_gethdr(Elf *e)
200{
201	Elf *parent;
202	struct ar_hdr *arh;
203	Elf_Arhdr *eh;
204	size_t n;
205
206	if ((parent = e->e_parent) == NULL) {
207		LIBELF_SET_ERROR(ARGUMENT, 0);
208		return (NULL);
209	}
210
211	arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));
212
213	assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
214	assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
215	    sizeof(struct ar_hdr));
216
217	if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
218		LIBELF_SET_ERROR(RESOURCE, 0);
219		return (NULL);
220	}
221
222	e->e_arhdr = eh;
223	eh->ar_name = eh->ar_rawname = NULL;
224
225	if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
226		 parent)) == NULL)
227		goto error;
228
229	if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
230		goto error;
231	eh->ar_uid = (uid_t) n;
232
233	if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
234		goto error;
235	eh->ar_gid = (gid_t) n;
236
237	if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
238		goto error;
239	eh->ar_mode = (mode_t) n;
240
241	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
242		goto error;
243	eh->ar_size = n;
244
245	if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
246		 sizeof(arh->ar_name), 1)) == NULL)
247		goto error;
248
249	return (eh);
250
251 error:
252	if (eh) {
253		if (eh->ar_name)
254			free(eh->ar_name);
255		if (eh->ar_rawname)
256			free(eh->ar_rawname);
257		free(eh);
258	}
259	e->e_arhdr = NULL;
260
261	return (NULL);
262}
263
264Elf *
265_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
266{
267	Elf *e;
268	off_t next;
269	struct ar_hdr *arh;
270	size_t sz;
271
272	assert(elf->e_kind == ELF_K_AR);
273
274	next = elf->e_u.e_ar.e_next;
275
276	/*
277	 * `next' is only set to zero by elf_next() when the last
278	 * member of an archive is processed.
279	 */
280	if (next == (off_t) 0)
281		return (NULL);
282
283	assert((next & 1) == 0);
284
285	arh = (struct ar_hdr *) (elf->e_rawfile + next);
286
287	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
288		LIBELF_SET_ERROR(ARCHIVE, 0);
289		return (NULL);
290	}
291
292	assert(sz > 0);
293
294	arh++;	/* skip over archive member header */
295
296	if ((e = elf_memory((char *) arh, sz)) == NULL)
297		return (NULL);
298
299	e->e_fd = fd;
300	e->e_cmd = c;
301
302	elf->e_u.e_ar.e_nchildren++;
303	e->e_parent = elf;
304
305	return (e);
306}
307
308Elf *
309_libelf_ar_open(Elf *e)
310{
311	int i;
312	char *s, *end;
313	size_t sz;
314	struct ar_hdr arh;
315
316	e->e_kind = ELF_K_AR;
317	e->e_u.e_ar.e_nchildren = 0;
318	e->e_u.e_ar.e_next = (off_t) -1;
319
320	/*
321	 * Look for special members.
322	 */
323
324	s = e->e_rawfile + SARMAG;
325	end = e->e_rawfile + e->e_rawsize;
326
327	assert(e->e_rawsize > 0);
328
329	/*
330	 * Look for magic names "/ " and "// " in the first two entries
331	 * of the archive.
332	 */
333	for (i = 0; i < 2; i++) {
334
335		if (s + sizeof(arh) > end) {
336			LIBELF_SET_ERROR(ARCHIVE, 0);
337			return (NULL);
338		}
339
340		(void) memcpy(&arh, s, sizeof(arh));
341
342		if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
343			LIBELF_SET_ERROR(ARCHIVE, 0);
344			return (NULL);
345		}
346
347		if (arh.ar_name[0] != '/')	/* not a special symbol */
348			break;
349
350		if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size), 10, &sz) == 0) {
351			LIBELF_SET_ERROR(ARCHIVE, 0);
352			return (NULL);
353		}
354
355		assert(sz > 0);
356
357		s += sizeof(arh);
358
359		if (arh.ar_name[1] == ' ') {	/* "/ " => symbol table */
360
361			e->e_u.e_ar.e_rawsymtab = s;
362			e->e_u.e_ar.e_rawsymtabsz = sz;
363
364		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
365
366			/* "// " => string table for long file names */
367			e->e_u.e_ar.e_rawstrtab = s;
368			e->e_u.e_ar.e_rawstrtabsz = sz;
369		}
370
371		sz = LIBELF_ADJUST_AR_SIZE(sz);
372
373		s += sz;
374	}
375
376	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
377
378	return (e);
379}
380
381/*
382 * An ar(1) symbol table has the following layout:
383 *
384 * The first 4 bytes are a binary count of the number of entries in the
385 * symbol table, stored MSB-first.
386 *
387 * Then there are 'n' 4-byte binary offsets, also stored MSB first.
388 *
389 * Following this, there are 'n' null-terminated strings.
390 */
391
392#define	GET_WORD(P, V) do {			\
393		(V) = 0;			\
394		(V) = (P)[0]; (V) <<= 8;	\
395		(V) += (P)[1]; (V) <<= 8;	\
396		(V) += (P)[2]; (V) <<= 8;	\
397		(V) += (P)[3];			\
398	} while (0)
399
400#define	INTSZ	4
401
402Elf_Arsym *
403_libelf_ar_process_symtab(Elf *e, size_t *count)
404{
405	size_t n, nentries, off;
406	Elf_Arsym *symtab, *sym;
407	unsigned char  *p, *s, *end;
408
409	assert(e != NULL);
410	assert(count != NULL);
411
412	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
413		LIBELF_SET_ERROR(ARCHIVE, 0);
414		return (NULL);
415	}
416
417	p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
418	end = p + e->e_u.e_ar.e_rawsymtabsz;
419
420	GET_WORD(p, nentries);
421	p += INTSZ;
422
423	if (nentries == 0 || p + nentries * INTSZ >= end) {
424		LIBELF_SET_ERROR(ARCHIVE, 0);
425		return (NULL);
426	}
427
428	/* Allocate space for a nentries + a sentinel. */
429	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
430		LIBELF_SET_ERROR(RESOURCE, 0);
431		return (NULL);
432	}
433
434	s = p + (nentries * INTSZ); /* start of the string table. */
435
436	for (n = nentries, sym = symtab; n > 0; n--) {
437		off = 0;
438
439		GET_WORD(p, off);
440
441		sym->as_off = off;
442		sym->as_hash = elf_hash(s);
443		sym->as_name = s;
444
445		p += INTSZ;
446		sym++;
447
448		for (; s < end && *s++ != '\0';) /* skip to next string */
449			;
450		if (s > end) {
451			LIBELF_SET_ERROR(ARCHIVE, 0);
452			free(symtab);
453			return (NULL);
454		}
455	}
456
457	/* Fill up the sentinel entry. */
458	sym->as_name = NULL;
459	sym->as_hash = ~0UL;
460	sym->as_off = (off_t) 0;
461
462	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
463	e->e_u.e_ar.e_symtab = symtab;
464
465	return (symtab);
466}
467