xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_ar_util.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1 /*	$NetBSD: libelf_ar_util.c,v 1.5 2024/03/03 17:37:34 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006,2009,2010 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
32 
33 #include <sys/cdefs.h>
34 
35 #include <assert.h>
36 #include <libelf.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "_libelf.h"
41 #include "_libelf_ar.h"
42 
43 __RCSID("$NetBSD: libelf_ar_util.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
44 ELFTC_VCSID("Id: libelf_ar_util.c 3977 2022-05-01 06:45:34Z jkoshy");
45 
46 /*
47  * Convert a string bounded by `start' and `start+sz' (exclusive) to a
48  * number in the specified base.
49  */
50 int
_libelf_ar_get_number(const char * src,size_t sz,unsigned int base,size_t * ret)51 _libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
52     size_t *ret)
53 {
54 	size_t r;
55 	unsigned int c, v;
56 	const unsigned char *e, *s;
57 
58 	assert(base <= 10);
59 
60 	s = (const unsigned char *) src;
61 	e = s + sz;
62 
63 	/* skip leading blanks */
64 	for (;s < e && (c = *s) == ' '; s++)
65 		;
66 
67 	r = 0L;
68 	for (;s < e; s++) {
69 		if ((c = *s) == ' ')
70 			break;
71 		if (c < '0' || c > '9')
72 			return (0);
73 		v = c - '0';
74 		if (v >= base)		/* Illegal digit. */
75 			break;
76 		r *= base;
77 		r += v;
78 	}
79 
80 	*ret = r;
81 
82 	return (1);
83 }
84 
85 /*
86  * Return the translated name for an archive member.
87  */
88 char *
_libelf_ar_get_translated_name(const struct ar_hdr * arh,Elf * ar)89 _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
90 {
91 	char *s;
92 	unsigned char c;
93 	size_t len, offset;
94 	const unsigned char *buf, *p, *q, *r;
95 	const size_t bufsize = sizeof(arh->ar_name);
96 
97 	assert(arh != NULL);
98 	assert(ar->e_kind == ELF_K_AR);
99 	assert((const unsigned char *) arh >= ar->e_rawfile &&
100 	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
101 
102 	buf = (const unsigned char *) arh->ar_name;
103 
104 	/*
105 	 * Check for extended naming.
106 	 *
107 	 * If the name matches the pattern "^/[0-9]+", it is an
108 	 * SVR4-style extended name.  If the name matches the pattern
109 	 * "#1/[0-9]+", the entry uses BSD style extended naming.
110 	 */
111 	if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
112 		/*
113 		 * The value in field ar_name is a decimal offset into
114 		 * the archive string table where the actual name
115 		 * resides.
116 		 */
117 		if (_libelf_ar_get_number((const char *) (buf + 1),
118 			bufsize - 1, 10, &offset) == 0) {
119 			LIBELF_SET_ERROR(ARCHIVE, 0);
120 			return (NULL);
121 		}
122 
123 		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
124 			LIBELF_SET_ERROR(ARCHIVE, 0);
125 			return (NULL);
126 		}
127 
128 		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
129 		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
130 
131 		for (; p < r && *p != '/'; p++)
132 			;
133 		len = (size_t) (p - q + 1); /* space for the trailing NUL */
134 
135 		if ((s = malloc(len)) == NULL) {
136 			LIBELF_SET_ERROR(RESOURCE, 0);
137 			return (NULL);
138 		}
139 
140 		(void) strncpy(s, (const char *) q, len - 1);
141 		s[len - 1] = '\0';
142 
143 		return (s);
144 	} else if (IS_EXTENDED_BSD_NAME(buf)) {
145 		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
146 
147 		if (_libelf_ar_get_number((const char *) r, bufsize -
148 			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
149 			&len) == 0) {
150 			LIBELF_SET_ERROR(ARCHIVE, 0);
151 			return (NULL);
152 		}
153 
154 		/*
155 		 * Allocate space for the file name plus a
156 		 * trailing NUL.
157 		 */
158 		if ((s = malloc(len + 1)) == NULL) {
159 			LIBELF_SET_ERROR(RESOURCE, 0);
160 			return (NULL);
161 		}
162 
163 		/*
164 		 * The file name follows the archive header.
165 		 */
166 		q = (const unsigned char *) (arh + 1);
167 
168 		(void) strncpy(s, (const char *) q, len);
169 		s[len] = '\0';
170 
171 		return (s);
172 	}
173 
174 	/*
175 	 * A 'normal' name.
176 	 *
177 	 * Skip back over trailing blanks from the end of the field.
178 	 * In the SVR4 format, a '/' is used as a terminator for
179 	 * non-special names.
180 	 */
181 	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
182 		;
183 
184 	if (q >= buf) {
185 		if (*q == '/') {
186 			/*
187 			 * SVR4 style names: ignore the trailing
188 			 * character '/', but only if the name is not
189 			 * one of the special names "/" and "//".
190 			 */
191 			if (q > buf + 1 ||
192 			    (q == (buf + 1) && *buf != '/'))
193 				q--;
194 		}
195 
196 		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
197 	} else {
198 		/* The buffer only had blanks. */
199 		buf = (const unsigned char *) "";
200 		len = 1;
201 	}
202 
203 	if ((s = malloc(len)) == NULL) {
204 		LIBELF_SET_ERROR(RESOURCE, 0);
205 		return (NULL);
206 	}
207 
208 	(void) strncpy(s, (const char *) buf, len - 1);
209 	s[len - 1] = '\0';
210 
211 	return (s);
212 }
213 
214 /*
215  * Return the raw name for an archive member, inclusive of any
216  * formatting characters.
217  */
218 char *
_libelf_ar_get_raw_name(const struct ar_hdr * arh)219 _libelf_ar_get_raw_name(const struct ar_hdr *arh)
220 {
221 	char *rawname;
222 	const size_t namesz = sizeof(arh->ar_name);
223 
224 	if ((rawname = malloc(namesz + 1)) == NULL) {
225 		LIBELF_SET_ERROR(RESOURCE, 0);
226 		return (NULL);
227 	}
228 
229 	(void) strncpy(rawname, arh->ar_name, namesz);
230 	rawname[namesz] = '\0';
231 	return (rawname);
232 }
233 
234 /*
235  * Open an 'ar' archive.
236  */
237 Elf *
_libelf_ar_open(Elf * e,int reporterror)238 _libelf_ar_open(Elf *e, int reporterror)
239 {
240 	size_t sz;
241 	int scanahead;
242 	struct ar_hdr arh;
243 	unsigned char *s, *end;
244 
245 	_libelf_init_elf(e, ELF_K_AR);
246 
247 	e->e_u.e_ar.e_nchildren = 0;
248 	e->e_u.e_ar.e_next = (off_t) -1;
249 
250 	/*
251 	 * Look for special members.
252 	 */
253 
254 	s = e->e_rawfile + SARMAG;
255 	end = e->e_rawfile + e->e_rawsize;
256 
257 	assert(e->e_rawsize > 0);
258 
259 	/*
260 	 * We use heuristics to determine the flavor of the archive we
261 	 * are examining.
262 	 *
263 	 * SVR4 flavor archives use the name "/ " and "// " for
264 	 * special members.
265 	 *
266 	 * In BSD flavor archives the symbol table, if present, is the
267 	 * first archive with name "__.SYMDEF".
268 	 */
269 
270 #define	READ_AR_HEADER(S, ARH, SZ, END)					\
271 	do {								\
272 		if ((S) + sizeof((ARH)) > (END))			\
273 		        goto error;					\
274 		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
275 		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
276 			goto error;					\
277 		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
278 		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
279 			goto error;					\
280 	} while (/* CONSTCOND */ 0)
281 
282 	READ_AR_HEADER(s, arh, sz, end);
283 
284 	/*
285 	 * Handle special archive members for the SVR4 format.
286 	 */
287 	if (arh.ar_name[0] == '/') {
288 		if (sz == 0)
289 			goto error;
290 
291 		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
292 
293 		scanahead = 0;
294 
295 		/*
296 		 * The symbol table (file name "/ ") always comes before the
297 		 * string table (file name "// ").
298 		 */
299 		if (arh.ar_name[1] == ' ') {
300 			/* "/ " => symbol table. */
301 			scanahead = 1;	/* The string table to follow. */
302 
303 			s += sizeof(arh);
304 			e->e_u.e_ar.e_rawsymtab = s;
305 			e->e_u.e_ar.e_rawsymtabsz = sz;
306 
307 			sz = LIBELF_ADJUST_AR_SIZE(sz);
308 			s += sz;
309 
310 		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
311 			/* "// " => string table for long file names. */
312 			s += sizeof(arh);
313 			e->e_u.e_ar.e_rawstrtab = s;
314 			e->e_u.e_ar.e_rawstrtabsz = sz;
315 
316 			sz = LIBELF_ADJUST_AR_SIZE(sz);
317 			s += sz;
318 		}
319 
320 		/*
321 		 * If the string table hasn't been seen yet, look for
322 		 * it in the next member.
323 		 */
324 		if (scanahead) {
325 			READ_AR_HEADER(s, arh, sz, end);
326 
327 			/* "// " => string table for long file names. */
328 			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
329 			    arh.ar_name[2] == ' ') {
330 
331 				s += sizeof(arh);
332 
333 				e->e_u.e_ar.e_rawstrtab = s;
334 				e->e_u.e_ar.e_rawstrtabsz = sz;
335 
336 				sz = LIBELF_ADJUST_AR_SIZE(sz);
337 				s += sz;
338 			}
339 		}
340 	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
341 		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
342 		/*
343 		 * BSD style archive symbol table.
344 		 */
345 		s += sizeof(arh);
346 		e->e_u.e_ar.e_rawsymtab = s;
347 		e->e_u.e_ar.e_rawsymtabsz = sz;
348 
349 		sz = LIBELF_ADJUST_AR_SIZE(sz);
350 		s += sz;
351 	}
352 
353 	/*
354 	 * Update the 'next' offset, so that a subsequent elf_begin()
355 	 * works as expected.
356 	 */
357 	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
358 
359 	return (e);
360 
361 error:
362 	if (!reporterror) {
363 		e->e_kind = ELF_K_NONE;
364 		return (e);
365 	}
366 
367 	LIBELF_SET_ERROR(ARCHIVE, 0);
368 	return (NULL);
369 }
370