2 * Copyright (c) 2006,2009,2010 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 #include "_libelf_ar.h"
35 ELFTC_VCSID("$Id: libelf_ar_util.c 3174 2015-03-27 17:13:41Z emaste $");
38 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
39 * number in the specified base.
42 _libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
47 const unsigned char *e, *s;
51 s = (const unsigned char *) src;
54 /* skip leading blanks */
55 for (;s < e && (c = *s) == ' '; s++)
62 if (c < '0' || c > '9')
65 if (v >= base) /* Illegal digit. */
77 * Return the translated name for an archive member.
80 _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
85 const unsigned char *buf, *p, *q, *r;
86 const size_t bufsize = sizeof(arh->ar_name);
89 assert(ar->e_kind == ELF_K_AR);
90 assert((const unsigned char *) arh >= ar->e_rawfile &&
91 (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
93 buf = (const unsigned char *) arh->ar_name;
96 * Check for extended naming.
98 * If the name matches the pattern "^/[0-9]+", it is an
99 * SVR4-style extended name. If the name matches the pattern
100 * "#1/[0-9]+", the entry uses BSD style extended naming.
102 if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
104 * The value in field ar_name is a decimal offset into
105 * the archive string table where the actual name
108 if (_libelf_ar_get_number((const char *) (buf + 1),
109 bufsize - 1, 10, &offset) == 0) {
110 LIBELF_SET_ERROR(ARCHIVE, 0);
114 if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
115 LIBELF_SET_ERROR(ARCHIVE, 0);
119 p = q = ar->e_u.e_ar.e_rawstrtab + offset;
120 r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
122 for (; p < r && *p != '/'; p++)
124 len = (size_t) (p - q + 1); /* space for the trailing NUL */
126 if ((s = malloc(len)) == NULL) {
127 LIBELF_SET_ERROR(RESOURCE, 0);
131 (void) strncpy(s, (const char *) q, len - 1);
135 } else if (IS_EXTENDED_BSD_NAME(buf)) {
136 r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
138 if (_libelf_ar_get_number((const char *) r, bufsize -
139 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
141 LIBELF_SET_ERROR(ARCHIVE, 0);
146 * Allocate space for the file name plus a
149 if ((s = malloc(len + 1)) == NULL) {
150 LIBELF_SET_ERROR(RESOURCE, 0);
155 * The file name follows the archive header.
157 q = (const unsigned char *) (arh + 1);
159 (void) strncpy(s, (const char *) q, len);
168 * Skip back over trailing blanks from the end of the field.
169 * In the SVR4 format, a '/' is used as a terminator for
172 for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
178 * SVR4 style names: ignore the trailing
179 * character '/', but only if the name is not
180 * one of the special names "/" and "//".
183 (q == (buf + 1) && *buf != '/'))
187 len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
189 /* The buffer only had blanks. */
190 buf = (const unsigned char *) "";
194 if ((s = malloc(len)) == NULL) {
195 LIBELF_SET_ERROR(RESOURCE, 0);
199 (void) strncpy(s, (const char *) buf, len - 1);
206 * Return the raw name for an archive member, inclusive of any
207 * formatting characters.
210 _libelf_ar_get_raw_name(const struct ar_hdr *arh)
213 const size_t namesz = sizeof(arh->ar_name);
215 if ((rawname = malloc(namesz + 1)) == NULL) {
216 LIBELF_SET_ERROR(RESOURCE, 0);
220 (void) strncpy(rawname, arh->ar_name, namesz);
221 rawname[namesz] = '\0';
226 * Open an 'ar' archive.
229 _libelf_ar_open(Elf *e, int reporterror)
234 unsigned char *s, *end;
236 _libelf_init_elf(e, ELF_K_AR);
238 e->e_u.e_ar.e_nchildren = 0;
239 e->e_u.e_ar.e_next = (off_t) -1;
242 * Look for special members.
245 s = e->e_rawfile + SARMAG;
246 end = e->e_rawfile + e->e_rawsize;
248 assert(e->e_rawsize > 0);
251 * We use heuristics to determine the flavor of the archive we
254 * SVR4 flavor archives use the name "/ " and "// " for
257 * In BSD flavor archives the symbol table, if present, is the
258 * first archive with name "__.SYMDEF".
261 #define READ_AR_HEADER(S, ARH, SZ, END) \
263 if ((S) + sizeof((ARH)) > (END)) \
265 (void) memcpy(&(ARH), (S), sizeof((ARH))); \
266 if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
268 if (_libelf_ar_get_number((char *) (ARH).ar_size, \
269 sizeof((ARH).ar_size), 10, &(SZ)) == 0) \
273 READ_AR_HEADER(s, arh, sz, end);
276 * Handle special archive members for the SVR4 format.
278 if (arh.ar_name[0] == '/') {
282 e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
287 * The symbol table (file name "/ ") always comes before the
288 * string table (file name "// ").
290 if (arh.ar_name[1] == ' ') {
291 /* "/ " => symbol table. */
292 scanahead = 1; /* The string table to follow. */
295 e->e_u.e_ar.e_rawsymtab = s;
296 e->e_u.e_ar.e_rawsymtabsz = sz;
298 sz = LIBELF_ADJUST_AR_SIZE(sz);
301 } else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
302 /* "// " => string table for long file names. */
304 e->e_u.e_ar.e_rawstrtab = s;
305 e->e_u.e_ar.e_rawstrtabsz = sz;
307 sz = LIBELF_ADJUST_AR_SIZE(sz);
312 * If the string table hasn't been seen yet, look for
313 * it in the next member.
316 READ_AR_HEADER(s, arh, sz, end);
318 /* "// " => string table for long file names. */
319 if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
320 arh.ar_name[2] == ' ') {
324 e->e_u.e_ar.e_rawstrtab = s;
325 e->e_u.e_ar.e_rawstrtabsz = sz;
327 sz = LIBELF_ADJUST_AR_SIZE(sz);
331 } else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
332 sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
334 * BSD style archive symbol table.
337 e->e_u.e_ar.e_rawsymtab = s;
338 e->e_u.e_ar.e_rawsymtabsz = sz;
340 sz = LIBELF_ADJUST_AR_SIZE(sz);
345 * Update the 'next' offset, so that a subsequent elf_begin()
348 e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
354 e->e_kind = ELF_K_NONE;
358 LIBELF_SET_ERROR(ARCHIVE, 0);