Home | History | Annotate | Line # | Download | only in libelf
      1 /*	$NetBSD: libelf_ar_util.c,v 1.7 2026/05/17 21:40:51 jkoshy 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 ELFTC_VCSID("Id: libelf_ar_util.c 4250 2025-10-18 18:28:04Z jkoshy");
     44 
     45 __RCSID("$NetBSD: libelf_ar_util.c,v 1.7 2026/05/17 21:40:51 jkoshy Exp $");
     46 
     47 /*
     48  * Convert a string bounded by `start' and `start+sz' (exclusive) to a
     49  * number in the specified base.
     50  */
     51 int
     52 _libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
     53     size_t *ret)
     54 {
     55 	size_t r;
     56 	unsigned int c, v;
     57 	const unsigned char *e, *s;
     58 
     59 	assert(base <= 10);
     60 
     61 	s = (const unsigned char *) src;
     62 	e = s + sz;
     63 
     64 	/* skip leading blanks */
     65 	for (;s < e && (c = *s) == ' '; s++)
     66 		;
     67 
     68 	r = 0L;
     69 	for (;s < e; s++) {
     70 		if ((c = *s) == ' ')
     71 			break;
     72 		if (c < '0' || c > '9')
     73 			return (0);
     74 		v = c - '0';
     75 		if (v >= base)		/* Illegal digit. */
     76 			return (0);
     77 		r *= base;
     78 		r += v;
     79 	}
     80 
     81 	*ret = r;
     82 
     83 	return (1);
     84 }
     85 
     86 /*
     87  * Return the translated name for an archive member.
     88  */
     89 char *
     90 _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
     91 {
     92 	char *s;
     93 	unsigned char c;
     94 	size_t len, offset;
     95 	const unsigned char *buf, *p, *q, *r;
     96 	const size_t bufsize = sizeof(arh->ar_name);
     97 
     98 	assert(arh != NULL);
     99 	assert(ar->e_kind == ELF_K_AR);
    100 	assert((const unsigned char *) arh >= ar->e_rawfile &&
    101 	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
    102 
    103 	buf = (const unsigned char *) arh->ar_name;
    104 
    105 	/*
    106 	 * Check for extended naming.
    107 	 *
    108 	 * If the name matches the pattern "^/[0-9]+", it is an
    109 	 * SVR4-style extended name.  If the name matches the pattern
    110 	 * "#1/[0-9]+", the entry uses BSD style extended naming.
    111 	 */
    112 	if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
    113 		/*
    114 		 * The value in field ar_name is a decimal offset into
    115 		 * the archive string table where the actual name
    116 		 * resides.
    117 		 */
    118 		if (_libelf_ar_get_number((const char *) (buf + 1),
    119 			bufsize - 1, 10, &offset) == 0) {
    120 			LIBELF_SET_ERROR(ARCHIVE, 0);
    121 			return (NULL);
    122 		}
    123 
    124 		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
    125 			LIBELF_SET_ERROR(ARCHIVE, 0);
    126 			return (NULL);
    127 		}
    128 
    129 		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
    130 		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
    131 
    132 		for (; p < r && *p != '/'; p++)
    133 			;
    134 		len = (size_t) (p - q + 1); /* space for the trailing NUL */
    135 
    136 		if ((s = malloc(len)) == NULL) {
    137 			LIBELF_SET_ERROR(RESOURCE, 0);
    138 			return (NULL);
    139 		}
    140 
    141 		(void) strncpy(s, (const char *) q, len - 1);
    142 		s[len - 1] = '\0';
    143 
    144 		return (s);
    145 	} else if (IS_EXTENDED_BSD_NAME(buf)) {
    146 		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
    147 
    148 		if (_libelf_ar_get_number((const char *) r, bufsize -
    149 			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
    150 			&len) == 0) {
    151 			LIBELF_SET_ERROR(ARCHIVE, 0);
    152 			return (NULL);
    153 		}
    154 
    155 		/*
    156 		 * Allocate space for the file name plus a
    157 		 * trailing NUL.
    158 		 */
    159 		if ((s = malloc(len + 1)) == NULL) {
    160 			LIBELF_SET_ERROR(RESOURCE, 0);
    161 			return (NULL);
    162 		}
    163 
    164 		/*
    165 		 * The file name follows the archive header.
    166 		 */
    167 		q = (const unsigned char *) (arh + 1);
    168 
    169 		(void) strncpy(s, (const char *) q, len);
    170 		s[len] = '\0';
    171 
    172 		return (s);
    173 	}
    174 
    175 	/*
    176 	 * A 'normal' name.
    177 	 *
    178 	 * Skip back over trailing blanks from the end of the field.
    179 	 * In the SVR4 format, a '/' is used as a terminator for
    180 	 * non-special names.
    181 	 */
    182 	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
    183 		;
    184 
    185 	if (q >= buf) {
    186 		if (*q == '/') {
    187 			/*
    188 			 * SVR4 style names: ignore the trailing
    189 			 * character '/', but only if the name is not
    190 			 * one of the special names "/" and "//".
    191 			 */
    192 			if (q > buf + 1 ||
    193 			    (q == (buf + 1) && *buf != '/'))
    194 				q--;
    195 		}
    196 
    197 		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
    198 	} else {
    199 		/* The buffer only had blanks. */
    200 		buf = (const unsigned char *) "";
    201 		len = 1;
    202 	}
    203 
    204 	if ((s = malloc(len)) == NULL) {
    205 		LIBELF_SET_ERROR(RESOURCE, 0);
    206 		return (NULL);
    207 	}
    208 
    209 	(void) strncpy(s, (const char *) buf, len - 1);
    210 	s[len - 1] = '\0';
    211 
    212 	return (s);
    213 }
    214 
    215 /*
    216  * Return the raw name for an archive member, inclusive of any
    217  * formatting characters.
    218  */
    219 char *
    220 _libelf_ar_get_raw_name(const struct ar_hdr *arh)
    221 {
    222 	char *rawname;
    223 	const size_t namesz = sizeof(arh->ar_name);
    224 
    225 	if ((rawname = malloc(namesz + 1)) == NULL) {
    226 		LIBELF_SET_ERROR(RESOURCE, 0);
    227 		return (NULL);
    228 	}
    229 
    230 	(void) strncpy(rawname, arh->ar_name, namesz);
    231 	rawname[namesz] = '\0';
    232 	return (rawname);
    233 }
    234 
    235 /*
    236  * Open an 'ar' archive.
    237  */
    238 Elf *
    239 _libelf_ar_open(Elf *e, int reporterror)
    240 {
    241 	size_t sz;
    242 	int scanahead;
    243 	struct ar_hdr arh;
    244 	unsigned char *s, *end;
    245 
    246 	_libelf_init_elf(e, ELF_K_AR);
    247 
    248 	e->e_u.e_ar.e_nchildren = 0;
    249 	e->e_u.e_ar.e_next = (off_t) -1;
    250 
    251 	/*
    252 	 * Look for special members.
    253 	 */
    254 
    255 	s = e->e_rawfile + SARMAG;
    256 	end = e->e_rawfile + e->e_rawsize;
    257 
    258 	assert(e->e_rawsize > 0);
    259 
    260 	/*
    261 	 * We use heuristics to determine the flavor of the archive we
    262 	 * are examining.
    263 	 *
    264 	 * SVR4 flavor archives use the name "/ " and "// " for
    265 	 * special members.
    266 	 *
    267 	 * In BSD flavor archives the symbol table, if present, is the
    268 	 * first archive with name "__.SYMDEF".
    269 	 */
    270 
    271 #define	READ_AR_HEADER(S, ARH, SZ, END)					\
    272 	do {								\
    273 		if ((S) + sizeof((ARH)) > (END))			\
    274 		        goto error;					\
    275 		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
    276 		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
    277 			goto error;					\
    278 		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
    279 		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
    280 			goto error;					\
    281 	} while (/* CONSTCOND */ 0)
    282 
    283 	READ_AR_HEADER(s, arh, sz, end);
    284 
    285 	/*
    286 	 * Handle special archive members for the SVR4 format.
    287 	 */
    288 	if (arh.ar_name[0] == '/') {
    289 		if (sz == 0)
    290 			goto error;
    291 
    292 		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
    293 
    294 		scanahead = 0;
    295 
    296 		/*
    297 		 * The symbol table (file name "/ ") always comes before the
    298 		 * string table (file name "// ").
    299 		 */
    300 		if (arh.ar_name[1] == ' ') {
    301 			/* "/ " => symbol table. */
    302 			scanahead = 1;	/* The string table to follow. */
    303 
    304 			s += sizeof(arh);
    305 			e->e_u.e_ar.e_rawsymtab = s;
    306 			e->e_u.e_ar.e_rawsymtabsz = sz;
    307 
    308 			sz = LIBELF_ADJUST_AR_SIZE(sz);
    309 			s += sz;
    310 
    311 		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
    312 			/* "// " => string table for long file names. */
    313 			s += sizeof(arh);
    314 			e->e_u.e_ar.e_rawstrtab = s;
    315 			e->e_u.e_ar.e_rawstrtabsz = sz;
    316 
    317 			sz = LIBELF_ADJUST_AR_SIZE(sz);
    318 			s += sz;
    319 		}
    320 
    321 		/*
    322 		 * If the string table hasn't been seen yet, look for
    323 		 * it in the next member.
    324 		 */
    325 		if (scanahead) {
    326 			READ_AR_HEADER(s, arh, sz, end);
    327 
    328 			/* "// " => string table for long file names. */
    329 			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
    330 			    arh.ar_name[2] == ' ') {
    331 
    332 				s += sizeof(arh);
    333 
    334 				e->e_u.e_ar.e_rawstrtab = s;
    335 				e->e_u.e_ar.e_rawstrtabsz = sz;
    336 
    337 				sz = LIBELF_ADJUST_AR_SIZE(sz);
    338 				s += sz;
    339 			}
    340 		}
    341 	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
    342 		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
    343 		/*
    344 		 * BSD style archive symbol table.
    345 		 */
    346 		s += sizeof(arh);
    347 		e->e_u.e_ar.e_rawsymtab = s;
    348 		e->e_u.e_ar.e_rawsymtabsz = sz;
    349 
    350 		sz = LIBELF_ADJUST_AR_SIZE(sz);
    351 		s += sz;
    352 	}
    353 
    354 	/*
    355 	 * Update the 'next' offset, so that a subsequent elf_begin()
    356 	 * works as expected.
    357 	 */
    358 	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
    359 
    360 	return (e);
    361 
    362 error:
    363 	if (!reporterror) {
    364 		e->e_kind = ELF_K_NONE;
    365 		return (e);
    366 	}
    367 
    368 	LIBELF_SET_ERROR(ARCHIVE, 0);
    369 	return (NULL);
    370 }
    371