138032Speter/*-
290792Sgshapiro * Copyright (c) 2006,2009,2010 Joseph Koshy
364562Sgshapiro * All rights reserved.
438032Speter *
538032Speter * Redistribution and use in source and binary forms, with or without
638032Speter * modification, are permitted provided that the following conditions
738032Speter * are met:
838032Speter * 1. Redistributions of source code must retain the above copyright
938032Speter *    notice, this list of conditions and the following disclaimer.
1038032Speter * 2. Redistributions in binary form must reproduce the above copyright
1138032Speter *    notice, this list of conditions and the following disclaimer in the
1238032Speter *    documentation and/or other materials provided with the distribution.
1338032Speter *
1464562Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
1538032Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16110560Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838032Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938032Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038032Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138032Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238032Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338032Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438032Speter * SUCH DAMAGE.
2538032Speter */
2638032Speter
2738032Speter#include <assert.h>
2838032Speter#include <libelf.h>
2938032Speter#include <stdlib.h>
3038032Speter#include <string.h>
3138032Speter
3238032Speter#include "_libelf.h"
3338032Speter#include "_libelf_ar.h"
3438032Speter
3538032SpeterELFTC_VCSID("$Id: libelf_ar_util.c,v 1.1 2019/02/01 05:27:38 jsg Exp $");
3638032Speter
3738032Speter/*
3838032Speter * Convert a string bounded by `start' and `start+sz' (exclusive) to a
3938032Speter * number in the specified base.
40110560Sgshapiro */
41110560Sgshapiroint
42110560Sgshapiro_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
43110560Sgshapiro    size_t *ret)
4438032Speter{
4538032Speter	size_t r;
4638032Speter	unsigned int c, v;
4738032Speter	const unsigned char *e, *s;
4838032Speter
4938032Speter	assert(base <= 10);
5038032Speter
5138032Speter	s = (const unsigned char *) src;
5238032Speter	e = s + sz;
5338032Speter
5438032Speter	/* skip leading blanks */
5538032Speter	for (;s < e && (c = *s) == ' '; s++)
5638032Speter		;
5738032Speter
5838032Speter	r = 0L;
5938032Speter	for (;s < e; s++) {
6038032Speter		if ((c = *s) == ' ')
6138032Speter			break;
6238032Speter		if (c < '0' || c > '9')
6338032Speter			return (0);
6438032Speter		v = c - '0';
6538032Speter		if (v >= base)		/* Illegal digit. */
6638032Speter			break;
6738032Speter		r *= base;
6838032Speter		r += v;
6938032Speter	}
7038032Speter
7138032Speter	*ret = r;
7238032Speter
7338032Speter	return (1);
7438032Speter}
7538032Speter
7638032Speter/*
7738032Speter * Return the translated name for an archive member.
7838032Speter */
7938032Speterchar *
8038032Speter_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
8138032Speter{
82110560Sgshapiro	char *s;
83110560Sgshapiro	unsigned char c;
8438032Speter	size_t len, offset;
8538032Speter	const unsigned char *buf, *p, *q, *r;
8638032Speter	const size_t bufsize = sizeof(arh->ar_name);
8738032Speter
8838032Speter	assert(arh != NULL);
8938032Speter	assert(ar->e_kind == ELF_K_AR);
9038032Speter	assert((const unsigned char *) arh >= ar->e_rawfile &&
9164562Sgshapiro	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
9238032Speter
9338032Speter	buf = (const unsigned char *) arh->ar_name;
9438032Speter
9538032Speter	/*
9638032Speter	 * Check for extended naming.
9738032Speter	 *
9838032Speter	 * If the name matches the pattern "^/[0-9]+", it is an
9938032Speter	 * SVR4-style extended name.  If the name matches the pattern
10038032Speter	 * "#1/[0-9]+", the entry uses BSD style extended naming.
10138032Speter	 */
10238032Speter	if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
10338032Speter		/*
10438032Speter		 * The value in field ar_name is a decimal offset into
10538032Speter		 * the archive string table where the actual name
10638032Speter		 * resides.
10738032Speter		 */
10838032Speter		if (_libelf_ar_get_number((const char *) (buf + 1),
10938032Speter			bufsize - 1, 10, &offset) == 0) {
11038032Speter			LIBELF_SET_ERROR(ARCHIVE, 0);
11138032Speter			return (NULL);
11238032Speter		}
11338032Speter
11438032Speter		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
11538032Speter			LIBELF_SET_ERROR(ARCHIVE, 0);
11638032Speter			return (NULL);
11738032Speter		}
11838032Speter
11938032Speter		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
12038032Speter		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
12138032Speter
12238032Speter		for (; p < r && *p != '/'; p++)
12338032Speter			;
12438032Speter		len = (size_t) (p - q + 1); /* space for the trailing NUL */
12538032Speter
12638032Speter		if ((s = malloc(len)) == NULL) {
12738032Speter			LIBELF_SET_ERROR(RESOURCE, 0);
12838032Speter			return (NULL);
12938032Speter		}
13038032Speter
13138032Speter		(void) strncpy(s, (const char *) q, len - 1);
13238032Speter		s[len - 1] = '\0';
13338032Speter
13438032Speter		return (s);
13538032Speter	} else if (IS_EXTENDED_BSD_NAME(buf)) {
13638032Speter		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
13738032Speter
138110560Sgshapiro		if (_libelf_ar_get_number((const char *) r, bufsize -
13938032Speter			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
14090792Sgshapiro			&len) == 0) {
14164562Sgshapiro			LIBELF_SET_ERROR(ARCHIVE, 0);
14264562Sgshapiro			return (NULL);
14364562Sgshapiro		}
14464562Sgshapiro
14564562Sgshapiro		/*
14664562Sgshapiro		 * Allocate space for the file name plus a
14764562Sgshapiro		 * trailing NUL.
14864562Sgshapiro		 */
14964562Sgshapiro		if ((s = malloc(len + 1)) == NULL) {
15064562Sgshapiro			LIBELF_SET_ERROR(RESOURCE, 0);
15164562Sgshapiro			return (NULL);
15264562Sgshapiro		}
15364562Sgshapiro
15464562Sgshapiro		/*
15564562Sgshapiro		 * The file name follows the archive header.
15690792Sgshapiro		 */
15764562Sgshapiro		q = (const unsigned char *) (arh + 1);
15864562Sgshapiro
15964562Sgshapiro		(void) strncpy(s, (const char *) q, len);
16090792Sgshapiro		s[len] = '\0';
16190792Sgshapiro
16290792Sgshapiro		return (s);
16390792Sgshapiro	}
16490792Sgshapiro
16590792Sgshapiro	/*
16664562Sgshapiro	 * A 'normal' name.
16764562Sgshapiro	 *
168	 * Skip back over trailing blanks from the end of the field.
169	 * In the SVR4 format, a '/' is used as a terminator for
170	 * non-special names.
171	 */
172	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
173		;
174
175	if (q >= buf) {
176		if (*q == '/') {
177			/*
178			 * SVR4 style names: ignore the trailing
179			 * character '/', but only if the name is not
180			 * one of the special names "/" and "//".
181			 */
182			if (q > buf + 1 ||
183			    (q == (buf + 1) && *buf != '/'))
184				q--;
185		}
186
187		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
188	} else {
189		/* The buffer only had blanks. */
190		buf = (const unsigned char *) "";
191		len = 1;
192	}
193
194	if ((s = malloc(len)) == NULL) {
195		LIBELF_SET_ERROR(RESOURCE, 0);
196		return (NULL);
197	}
198
199	(void) strncpy(s, (const char *) buf, len - 1);
200	s[len - 1] = '\0';
201
202	return (s);
203}
204
205/*
206 * Return the raw name for an archive member, inclusive of any
207 * formatting characters.
208 */
209char *
210_libelf_ar_get_raw_name(const struct ar_hdr *arh)
211{
212	char *rawname;
213	const size_t namesz = sizeof(arh->ar_name);
214
215	if ((rawname = malloc(namesz + 1)) == NULL) {
216		LIBELF_SET_ERROR(RESOURCE, 0);
217		return (NULL);
218	}
219
220	(void) strncpy(rawname, arh->ar_name, namesz);
221	rawname[namesz] = '\0';
222	return (rawname);
223}
224
225/*
226 * Open an 'ar' archive.
227 */
228Elf *
229_libelf_ar_open(Elf *e, int reporterror)
230{
231	size_t sz;
232	int scanahead;
233	struct ar_hdr arh;
234	unsigned char *s, *end;
235
236	_libelf_init_elf(e, ELF_K_AR);
237
238	e->e_u.e_ar.e_nchildren = 0;
239	e->e_u.e_ar.e_next = (off_t) -1;
240
241	/*
242	 * Look for special members.
243	 */
244
245	s = e->e_rawfile + SARMAG;
246	end = e->e_rawfile + e->e_rawsize;
247
248	assert(e->e_rawsize > 0);
249
250	/*
251	 * We use heuristics to determine the flavor of the archive we
252	 * are examining.
253	 *
254	 * SVR4 flavor archives use the name "/ " and "// " for
255	 * special members.
256	 *
257	 * In BSD flavor archives the symbol table, if present, is the
258	 * first archive with name "__.SYMDEF".
259	 */
260
261#define	READ_AR_HEADER(S, ARH, SZ, END)					\
262	do {								\
263		if ((S) + sizeof((ARH)) > (END))			\
264		        goto error;					\
265		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
266		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
267			goto error;					\
268		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
269		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
270			goto error;					\
271	} while (0)
272
273	READ_AR_HEADER(s, arh, sz, end);
274
275	/*
276	 * Handle special archive members for the SVR4 format.
277	 */
278	if (arh.ar_name[0] == '/') {
279		if (sz == 0)
280			goto error;
281
282		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
283
284		scanahead = 0;
285
286		/*
287		 * The symbol table (file name "/ ") always comes before the
288		 * string table (file name "// ").
289		 */
290		if (arh.ar_name[1] == ' ') {
291			/* "/ " => symbol table. */
292			scanahead = 1;	/* The string table to follow. */
293
294			s += sizeof(arh);
295			e->e_u.e_ar.e_rawsymtab = s;
296			e->e_u.e_ar.e_rawsymtabsz = sz;
297
298			sz = LIBELF_ADJUST_AR_SIZE(sz);
299			s += sz;
300
301		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
302			/* "// " => string table for long file names. */
303			s += sizeof(arh);
304			e->e_u.e_ar.e_rawstrtab = s;
305			e->e_u.e_ar.e_rawstrtabsz = sz;
306
307			sz = LIBELF_ADJUST_AR_SIZE(sz);
308			s += sz;
309		}
310
311		/*
312		 * If the string table hasn't been seen yet, look for
313		 * it in the next member.
314		 */
315		if (scanahead) {
316			READ_AR_HEADER(s, arh, sz, end);
317
318			/* "// " => string table for long file names. */
319			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
320			    arh.ar_name[2] == ' ') {
321
322				s += sizeof(arh);
323
324				e->e_u.e_ar.e_rawstrtab = s;
325				e->e_u.e_ar.e_rawstrtabsz = sz;
326
327				sz = LIBELF_ADJUST_AR_SIZE(sz);
328				s += sz;
329			}
330		}
331	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
332		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
333		/*
334		 * BSD style archive symbol table.
335		 */
336		s += sizeof(arh);
337		e->e_u.e_ar.e_rawsymtab = s;
338		e->e_u.e_ar.e_rawsymtabsz = sz;
339
340		sz = LIBELF_ADJUST_AR_SIZE(sz);
341		s += sz;
342	}
343
344	/*
345	 * Update the 'next' offset, so that a subsequent elf_begin()
346	 * works as expected.
347	 */
348	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
349
350	return (e);
351
352error:
353	if (!reporterror) {
354		e->e_kind = ELF_K_NONE;
355		return (e);
356	}
357
358	LIBELF_SET_ERROR(ARCHIVE, 0);
359	return (NULL);
360}
361