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