1 /* $NetBSD: libelf_ar_util.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2009,2010 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
33 #include <sys/cdefs.h>
41 #include "_libelf_ar.h"
43 __RCSID("$NetBSD: libelf_ar_util.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
44 ELFTC_VCSID("Id: libelf_ar_util.c 2365 2011-12-29 04:36:44Z jkoshy ");
47 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
48 * number in the specified base.
51 _libelf_ar_get_number(const char *s
, size_t sz
, int base
, size_t *ret
)
61 /* skip leading blanks */
62 for (;s
< e
&& (c
= *s
) == ' '; s
++)
69 if (c
< '0' || c
> '9')
72 if (v
>= base
) /* Illegal digit. */
84 * Return the translated name for an archive member.
87 _libelf_ar_get_translated_name(const struct ar_hdr
*arh
, Elf
*ar
)
91 const char *buf
, *p
, *q
, *r
;
92 const size_t bufsize
= sizeof(arh
->ar_name
);
95 assert(ar
->e_kind
== ELF_K_AR
);
96 assert((const char *) arh
>= ar
->e_rawfile
&&
97 (const char *) arh
< ar
->e_rawfile
+ ar
->e_rawsize
);
102 * Check for extended naming.
104 * If the name matches the pattern "^/[0-9]+", it is an
105 * SVR4-style extended name. If the name matches the pattern
106 * "#1/[0-9]+", the entry uses BSD style extended naming.
108 if (buf
[0] == '/' && (c
= buf
[1]) >= '0' && c
<= '9') {
110 * The value in field ar_name is a decimal offset into
111 * the archive string table where the actual name
114 if (_libelf_ar_get_number(buf
+ 1, bufsize
- 1, 10,
116 LIBELF_SET_ERROR(ARCHIVE
, 0);
120 if (offset
> ar
->e_u
.e_ar
.e_rawstrtabsz
) {
121 LIBELF_SET_ERROR(ARCHIVE
, 0);
125 p
= q
= ar
->e_u
.e_ar
.e_rawstrtab
+ offset
;
126 r
= ar
->e_u
.e_ar
.e_rawstrtab
+ ar
->e_u
.e_ar
.e_rawstrtabsz
;
128 for (; p
< r
&& *p
!= '/'; p
++)
130 len
= p
- q
+ 1; /* space for the trailing NUL */
132 if ((s
= malloc(len
)) == NULL
) {
133 LIBELF_SET_ERROR(RESOURCE
, 0);
137 (void) strncpy(s
, q
, len
- 1);
141 } else if (IS_EXTENDED_BSD_NAME(buf
)) {
142 r
= buf
+ LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE
;
144 if (_libelf_ar_get_number(r
, bufsize
-
145 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE
, 10,
147 LIBELF_SET_ERROR(ARCHIVE
, 0);
152 * Allocate space for the file name plus a
155 if ((s
= malloc(len
+ 1)) == NULL
) {
156 LIBELF_SET_ERROR(RESOURCE
, 0);
161 * The file name follows the archive header.
163 q
= (const char *) (arh
+ 1);
165 (void) strncpy(s
, q
, len
);
174 * Skip back over trailing blanks from the end of the field.
175 * In the SVR4 format, a '/' is used as a terminator for
178 for (q
= buf
+ bufsize
- 1; q
>= buf
&& *q
== ' '; --q
)
184 * SVR4 style names: ignore the trailing
185 * character '/', but only if the name is not
186 * one of the special names "/" and "//".
189 (q
== (buf
+ 1) && *buf
!= '/'))
193 len
= q
- buf
+ 2; /* Add space for a trailing NUL. */
195 /* The buffer only had blanks. */
200 if ((s
= malloc(len
)) == NULL
) {
201 LIBELF_SET_ERROR(RESOURCE
, 0);
205 (void) strncpy(s
, buf
, len
- 1);
212 * Return the raw name for an archive member, inclusive of any
213 * formatting characters.
216 _libelf_ar_get_raw_name(const struct ar_hdr
*arh
)
219 const size_t namesz
= sizeof(arh
->ar_name
);
221 if ((rawname
= malloc(namesz
+ 1)) == NULL
) {
222 LIBELF_SET_ERROR(RESOURCE
, 0);
226 (void) strncpy(rawname
, arh
->ar_name
, namesz
);
227 rawname
[namesz
] = '\0';
232 * Open an 'ar' archive.
235 _libelf_ar_open(Elf
*e
, int reporterror
)
242 _libelf_init_elf(e
, ELF_K_AR
);
244 e
->e_u
.e_ar
.e_nchildren
= 0;
245 e
->e_u
.e_ar
.e_next
= (off_t
) -1;
248 * Look for special members.
251 s
= e
->e_rawfile
+ SARMAG
;
252 end
= e
->e_rawfile
+ e
->e_rawsize
;
254 assert(e
->e_rawsize
> 0);
257 * We use heuristics to determine the flavor of the archive we
260 * SVR4 flavor archives use the name "/ " and "// " for
263 * In BSD flavor archives the symbol table, if present, is the
264 * first archive with name "__.SYMDEF".
267 #define READ_AR_HEADER(S, ARH, SZ, END) \
269 if ((S) + sizeof((ARH)) > (END)) \
271 (void) memcpy(&(ARH), (S), sizeof((ARH))); \
272 if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
274 if (_libelf_ar_get_number((ARH).ar_size, \
275 sizeof((ARH).ar_size), 10, &(SZ)) == 0) \
279 READ_AR_HEADER(s
, arh
, sz
, end
);
282 * Handle special archive members for the SVR4 format.
284 if (arh
.ar_name
[0] == '/') {
288 e
->e_flags
|= LIBELF_F_AR_VARIANT_SVR4
;
293 * The symbol table (file name "/ ") always comes before the
294 * string table (file name "// ").
296 if (arh
.ar_name
[1] == ' ') {
297 /* "/ " => symbol table. */
298 scanahead
= 1; /* The string table to follow. */
301 e
->e_u
.e_ar
.e_rawsymtab
= s
;
302 e
->e_u
.e_ar
.e_rawsymtabsz
= sz
;
304 sz
= LIBELF_ADJUST_AR_SIZE(sz
);
307 } else if (arh
.ar_name
[1] == '/' && arh
.ar_name
[2] == ' ') {
308 /* "// " => string table for long file names. */
310 e
->e_u
.e_ar
.e_rawstrtab
= s
;
311 e
->e_u
.e_ar
.e_rawstrtabsz
= sz
;
313 sz
= LIBELF_ADJUST_AR_SIZE(sz
);
318 * If the string table hasn't been seen yet, look for
319 * it in the next member.
322 READ_AR_HEADER(s
, arh
, sz
, end
);
324 /* "// " => string table for long file names. */
325 if (arh
.ar_name
[0] == '/' && arh
.ar_name
[1] == '/' &&
326 arh
.ar_name
[2] == ' ') {
330 e
->e_u
.e_ar
.e_rawstrtab
= s
;
331 e
->e_u
.e_ar
.e_rawstrtabsz
= sz
;
333 sz
= LIBELF_ADJUST_AR_SIZE(sz
);
337 } else if (strncmp(arh
.ar_name
, LIBELF_AR_BSD_SYMTAB_NAME
,
338 sizeof(LIBELF_AR_BSD_SYMTAB_NAME
) - 1) == 0) {
340 * BSD style archive symbol table.
343 e
->e_u
.e_ar
.e_rawsymtab
= s
;
344 e
->e_u
.e_ar
.e_rawsymtabsz
= sz
;
346 sz
= LIBELF_ADJUST_AR_SIZE(sz
);
351 * Update the 'next' offset, so that a subsequent elf_begin()
354 e
->e_u
.e_ar
.e_next
= (off_t
) (s
- e
->e_rawfile
);
360 e
->e_kind
= ELF_K_NONE
;
364 LIBELF_SET_ERROR(ARCHIVE
, 0);