summaryrefslogtreecommitdiff
path: root/lib/libelf/libelf_ar_util.c
blob: 83ff9cc142ef44e7a6b12f36e5ab5c6e1ca458ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*-
 * Copyright (c) 2006,2009,2010 Joseph Koshy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <assert.h>
#include <libelf.h>
#include <stdlib.h>
#include <string.h>

#include "_libelf.h"
#include "_libelf_ar.h"

ELFTC_VCSID("$Id: libelf_ar_util.c,v 1.1 2019/02/01 05:27:38 jsg Exp $");

/*
 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
 * number in the specified base.
 */
int
_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
    size_t *ret)
{
	size_t r;
	unsigned int c, v;
	const unsigned char *e, *s;

	assert(base <= 10);

	s = (const unsigned char *) src;
	e = s + sz;

	/* skip leading blanks */
	for (;s < e && (c = *s) == ' '; s++)
		;

	r = 0L;
	for (;s < e; s++) {
		if ((c = *s) == ' ')
			break;
		if (c < '0' || c > '9')
			return (0);
		v = c - '0';
		if (v >= base)		/* Illegal digit. */
			break;
		r *= base;
		r += v;
	}

	*ret = r;

	return (1);
}

/*
 * Return the translated name for an archive member.
 */
char *
_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
{
	char *s;
	unsigned char c;
	size_t len, offset;
	const unsigned char *buf, *p, *q, *r;
	const size_t bufsize = sizeof(arh->ar_name);

	assert(arh != NULL);
	assert(ar->e_kind == ELF_K_AR);
	assert((const unsigned char *) arh >= ar->e_rawfile &&
	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);

	buf = (const unsigned char *) arh->ar_name;

	/*
	 * Check for extended naming.
	 *
	 * If the name matches the pattern "^/[0-9]+", it is an
	 * SVR4-style extended name.  If the name matches the pattern
	 * "#1/[0-9]+", the entry uses BSD style extended naming.
	 */
	if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
		/*
		 * The value in field ar_name is a decimal offset into
		 * the archive string table where the actual name
		 * resides.
		 */
		if (_libelf_ar_get_number((const char *) (buf + 1),
			bufsize - 1, 10, &offset) == 0) {
			LIBELF_SET_ERROR(ARCHIVE, 0);
			return (NULL);
		}

		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
			LIBELF_SET_ERROR(ARCHIVE, 0);
			return (NULL);
		}

		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;

		for (; p < r && *p != '/'; p++)
			;
		len = (size_t) (p - q + 1); /* space for the trailing NUL */

		if ((s = malloc(len)) == NULL) {
			LIBELF_SET_ERROR(RESOURCE, 0);
			return (NULL);
		}

		(void) strncpy(s, (const char *) q, len - 1);
		s[len - 1] = '\0';

		return (s);
	} else if (IS_EXTENDED_BSD_NAME(buf)) {
		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;

		if (_libelf_ar_get_number((const char *) r, bufsize -
			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
			&len) == 0) {
			LIBELF_SET_ERROR(ARCHIVE, 0);
			return (NULL);
		}

		/*
		 * Allocate space for the file name plus a
		 * trailing NUL.
		 */
		if ((s = malloc(len + 1)) == NULL) {
			LIBELF_SET_ERROR(RESOURCE, 0);
			return (NULL);
		}

		/*
		 * The file name follows the archive header.
		 */
		q = (const unsigned char *) (arh + 1);

		(void) strncpy(s, (const char *) q, len);
		s[len] = '\0';

		return (s);
	}

	/*
	 * A 'normal' name.
	 *
	 * Skip back over trailing blanks from the end of the field.
	 * In the SVR4 format, a '/' is used as a terminator for
	 * non-special names.
	 */
	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
		;

	if (q >= buf) {
		if (*q == '/') {
			/*
			 * SVR4 style names: ignore the trailing
			 * character '/', but only if the name is not
			 * one of the special names "/" and "//".
			 */
			if (q > buf + 1 ||
			    (q == (buf + 1) && *buf != '/'))
				q--;
		}

		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
	} else {
		/* The buffer only had blanks. */
		buf = (const unsigned char *) "";
		len = 1;
	}

	if ((s = malloc(len)) == NULL) {
		LIBELF_SET_ERROR(RESOURCE, 0);
		return (NULL);
	}

	(void) strncpy(s, (const char *) buf, len - 1);
	s[len - 1] = '\0';

	return (s);
}

/*
 * Return the raw name for an archive member, inclusive of any
 * formatting characters.
 */
char *
_libelf_ar_get_raw_name(const struct ar_hdr *arh)
{
	char *rawname;
	const size_t namesz = sizeof(arh->ar_name);

	if ((rawname = malloc(namesz + 1)) == NULL) {
		LIBELF_SET_ERROR(RESOURCE, 0);
		return (NULL);
	}

	(void) strncpy(rawname, arh->ar_name, namesz);
	rawname[namesz] = '\0';
	return (rawname);
}

/*
 * Open an 'ar' archive.
 */
Elf *
_libelf_ar_open(Elf *e, int reporterror)
{
	size_t sz;
	int scanahead;
	struct ar_hdr arh;
	unsigned char *s, *end;

	_libelf_init_elf(e, ELF_K_AR);

	e->e_u.e_ar.e_nchildren = 0;
	e->e_u.e_ar.e_next = (off_t) -1;

	/*
	 * Look for special members.
	 */

	s = e->e_rawfile + SARMAG;
	end = e->e_rawfile + e->e_rawsize;

	assert(e->e_rawsize > 0);

	/*
	 * We use heuristics to determine the flavor of the archive we
	 * are examining.
	 *
	 * SVR4 flavor archives use the name "/ " and "// " for
	 * special members.
	 *
	 * In BSD flavor archives the symbol table, if present, is the
	 * first archive with name "__.SYMDEF".
	 */

#define	READ_AR_HEADER(S, ARH, SZ, END)					\
	do {								\
		if ((S) + sizeof((ARH)) > (END))			\
		        goto error;					\
		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
			goto error;					\
		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
			goto error;					\
	} while (0)

	READ_AR_HEADER(s, arh, sz, end);

	/*
	 * Handle special archive members for the SVR4 format.
	 */
	if (arh.ar_name[0] == '/') {
		if (sz == 0)
			goto error;

		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;

		scanahead = 0;

		/*
		 * The symbol table (file name "/ ") always comes before the
		 * string table (file name "// ").
		 */
		if (arh.ar_name[1] == ' ') {
			/* "/ " => symbol table. */
			scanahead = 1;	/* The string table to follow. */

			s += sizeof(arh);
			e->e_u.e_ar.e_rawsymtab = s;
			e->e_u.e_ar.e_rawsymtabsz = sz;

			sz = LIBELF_ADJUST_AR_SIZE(sz);
			s += sz;

		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
			/* "// " => string table for long file names. */
			s += sizeof(arh);
			e->e_u.e_ar.e_rawstrtab = s;
			e->e_u.e_ar.e_rawstrtabsz = sz;

			sz = LIBELF_ADJUST_AR_SIZE(sz);
			s += sz;
		}

		/*
		 * If the string table hasn't been seen yet, look for
		 * it in the next member.
		 */
		if (scanahead) {
			READ_AR_HEADER(s, arh, sz, end);

			/* "// " => string table for long file names. */
			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
			    arh.ar_name[2] == ' ') {

				s += sizeof(arh);

				e->e_u.e_ar.e_rawstrtab = s;
				e->e_u.e_ar.e_rawstrtabsz = sz;

				sz = LIBELF_ADJUST_AR_SIZE(sz);
				s += sz;
			}
		}
	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
		/*
		 * BSD style archive symbol table.
		 */
		s += sizeof(arh);
		e->e_u.e_ar.e_rawsymtab = s;
		e->e_u.e_ar.e_rawsymtabsz = sz;

		sz = LIBELF_ADJUST_AR_SIZE(sz);
		s += sz;
	}

	/*
	 * Update the 'next' offset, so that a subsequent elf_begin()
	 * works as expected.
	 */
	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);

	return (e);

error:
	if (!reporterror) {
		e->e_kind = ELF_K_NONE;
		return (e);
	}

	LIBELF_SET_ERROR(ARCHIVE, 0);
	return (NULL);
}