Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libelf / dist / libelf_ar.c
blob6f4f0c9d5f4e50109222be0d0edbaf6046059d2c
1 /* $NetBSD: libelf_ar.c,v 1.3 2009/12/19 07:59:24 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2006 Joseph Koshy
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/libelf_ar.c,v 1.3.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <ar.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <libelf.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "_libelf.h"
41 #define LIBELF_NALLOC_SIZE 16
44 * `ar' archive handling.
46 * `ar' archives start with signature `ARMAG'. Each archive member is
47 * preceded by a header containing meta-data for the member. This
48 * header is described in <ar.h> (struct ar_hdr). The header always
49 * starts on an even address. File data is padded with "\n"
50 * characters to keep this invariant.
52 * Special considerations for `ar' archives:
54 * The `ar' header only has space for a 16 character file name. File
55 * names are terminated with a '/', so this effectively leaves 15
56 * characters for the actual file name. In order to accomodate longer
57 * file names, names may be stored in a separate 'string table' and
58 * referenced indirectly by a member header. The string table itself
59 * appears as an archive member with name "// ". An indirect file name
60 * in an `ar' header matches the pattern "/[0-9]*". The digits form a
61 * decimal number that corresponds to a byte offset into the string
62 * table where the actual file name of the object starts. Strings in
63 * the string table are padded to start on even addresses.
65 * Archives may also have a symbol table (see ranlib(1)), mapping
66 * program symbols to object files inside the archive. A symbol table
67 * uses a file name of "/ " in its archive header. The symbol table
68 * is structured as:
69 * - a 4-byte count of entries stored as a binary value, MSB first
70 * - 'n' 4-byte offsets, stored as binary values, MSB first
71 * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
73 * If the symbol table and string table are is present in an archive
74 * they must be the very first objects and in that order.
78 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
79 * number in the specified base.
81 static int
82 _libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
84 int c, v;
85 size_t r;
86 char *e;
88 assert(base <= 10);
90 e = s + sz;
92 /* skip leading blanks */
93 for (;s < e && (c = *s) == ' '; s++)
96 r = 0L;
97 for (;s < e; s++) {
98 if ((c = *s) == ' ')
99 break;
100 if (c < '0' || c > '9')
101 return (0);
102 v = c - '0';
103 if (v >= base) /* Illegal digit. */
104 break;
105 r *= base;
106 r += v;
109 *ret = r;
111 return (1);
115 * Retrieve a string from a name field. If `rawname' is set, leave
116 * ar(1) control characters in.
118 static char *
119 _libelf_ar_get_string(const char *buf, size_t bufsize, int rawname)
121 const char *q;
122 char *r;
123 size_t sz;
125 if (rawname)
126 sz = bufsize + 1;
127 else {
128 /* Skip back over trailing blanks. */
129 for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
132 if (q < buf) {
134 * If the input buffer only had blanks in it,
135 * return a zero-length string.
137 buf = "";
138 sz = 1;
139 } else {
141 * Remove the trailing '/' character, but only
142 * if the name isn't one of the special names
143 * "/" and "//".
145 if (q > buf + 1 ||
146 (q == (buf + 1) && *buf != '/'))
147 q--;
149 sz = q - buf + 2; /* Space for a trailing NUL. */
153 if ((r = malloc(sz)) == NULL) {
154 LIBELF_SET_ERROR(RESOURCE, 0);
155 return (NULL);
158 (void) strncpy(r, buf, sz);
159 r[sz - 1] = '\0';
161 return (r);
165 * Retrieve the full name of the archive member.
167 static char *
168 _libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
170 char c, *q, *r, *s;
171 size_t len;
172 size_t offset;
174 assert(e->e_kind == ELF_K_AR);
176 if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
178 * The value in field ar_name is a decimal offset into
179 * the archive string table where the actual name
180 * resides.
182 if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
183 &offset) == 0) {
184 LIBELF_SET_ERROR(ARCHIVE, 0);
185 return (NULL);
188 if (offset > e->e_u.e_ar.e_rawstrtabsz) {
189 LIBELF_SET_ERROR(ARCHIVE, 0);
190 return (NULL);
193 s = q = e->e_u.e_ar.e_rawstrtab + offset;
194 r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;
196 for (s = q; s < r && *s != '/'; s++)
198 len = s - q + 1; /* space for the trailing NUL */
200 if ((s = malloc(len)) == NULL) {
201 LIBELF_SET_ERROR(RESOURCE, 0);
202 return (NULL);
205 (void) strncpy(s, q, len);
206 s[len - 1] = '\0';
208 return (s);
212 * Normal 'name'
214 return (_libelf_ar_get_string(buf, bufsize, 0));
218 Elf_Arhdr *
219 _libelf_ar_gethdr(Elf *e)
221 Elf *parent;
222 struct ar_hdr *arh;
223 Elf_Arhdr *eh;
224 size_t n;
226 if ((parent = e->e_parent) == NULL) {
227 LIBELF_SET_ERROR(ARGUMENT, 0);
228 return (NULL);
231 arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));
233 assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
234 assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
235 sizeof(struct ar_hdr));
237 if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
238 LIBELF_SET_ERROR(RESOURCE, 0);
239 return (NULL);
242 e->e_arhdr = eh;
243 eh->ar_name = eh->ar_rawname = NULL;
245 if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
246 parent)) == NULL)
247 goto error;
249 if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
250 goto error;
251 eh->ar_uid = (uid_t) n;
253 if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
254 goto error;
255 eh->ar_gid = (gid_t) n;
257 if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
258 goto error;
259 eh->ar_mode = (mode_t) n;
261 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
262 goto error;
263 eh->ar_size = n;
265 if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
266 sizeof(arh->ar_name), 1)) == NULL)
267 goto error;
269 return (eh);
271 error:
272 if (eh) {
273 if (eh->ar_name)
274 free(eh->ar_name);
275 if (eh->ar_rawname)
276 free(eh->ar_rawname);
277 free(eh);
279 e->e_arhdr = NULL;
281 return (NULL);
284 Elf *
285 _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
287 Elf *e;
288 off_t next;
289 struct ar_hdr *arh;
290 size_t sz;
292 assert(elf->e_kind == ELF_K_AR);
294 next = elf->e_u.e_ar.e_next;
297 * `next' is only set to zero by elf_next() when the last
298 * member of an archive is processed.
300 if (next == (off_t) 0)
301 return (NULL);
303 assert((next & 1) == 0);
305 arh = (struct ar_hdr *) (elf->e_rawfile + next);
307 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
308 LIBELF_SET_ERROR(ARCHIVE, 0);
309 return (NULL);
312 assert(sz > 0);
314 arh++; /* skip over archive member header */
316 if ((e = elf_memory((char *) arh, sz)) == NULL)
317 return (NULL);
319 e->e_fd = fd;
320 e->e_cmd = c;
322 elf->e_u.e_ar.e_nchildren++;
323 e->e_parent = elf;
325 return (e);
328 Elf *
329 _libelf_ar_open(Elf *e)
331 int i;
332 char *s, *end;
333 size_t sz;
334 struct ar_hdr arh;
336 e->e_kind = ELF_K_AR;
337 e->e_u.e_ar.e_nchildren = 0;
338 e->e_u.e_ar.e_next = (off_t) -1;
341 * Look for special members.
344 s = e->e_rawfile + SARMAG;
345 end = e->e_rawfile + e->e_rawsize;
347 assert(e->e_rawsize > 0);
350 * Look for magic names "/ " and "// " in the first two entries
351 * of the archive.
353 for (i = 0; i < 2; i++) {
355 if (s + sizeof(arh) > end) {
356 LIBELF_SET_ERROR(ARCHIVE, 0);
357 return (NULL);
360 (void) memcpy(&arh, s, sizeof(arh));
362 if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
363 LIBELF_SET_ERROR(ARCHIVE, 0);
364 return (NULL);
367 if (arh.ar_name[0] != '/') /* not a special symbol */
368 break;
370 if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size), 10, &sz) == 0) {
371 LIBELF_SET_ERROR(ARCHIVE, 0);
372 return (NULL);
375 assert(sz > 0);
377 s += sizeof(arh);
379 if (arh.ar_name[1] == ' ') { /* "/ " => symbol table */
381 e->e_u.e_ar.e_rawsymtab = s;
382 e->e_u.e_ar.e_rawsymtabsz = sz;
384 } else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
386 /* "// " => string table for long file names */
387 e->e_u.e_ar.e_rawstrtab = s;
388 e->e_u.e_ar.e_rawstrtabsz = sz;
391 sz = LIBELF_ADJUST_AR_SIZE(sz);
393 s += sz;
396 e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
398 return (e);
402 * An ar(1) symbol table has the following layout:
404 * The first 4 bytes are a binary count of the number of entries in the
405 * symbol table, stored MSB-first.
407 * Then there are 'n' 4-byte binary offsets, also stored MSB first.
409 * Following this, there are 'n' null-terminated strings.
412 #define GET_WORD(P, V) do { \
413 (V) = 0; \
414 (V) = (P)[0]; (V) <<= 8; \
415 (V) += (P)[1]; (V) <<= 8; \
416 (V) += (P)[2]; (V) <<= 8; \
417 (V) += (P)[3]; \
418 } while (/*CONSTCOND*/0)
420 #define INTSZ 4
422 Elf_Arsym *
423 _libelf_ar_process_symtab(Elf *e, size_t *count)
425 size_t n, nentries, off;
426 Elf_Arsym *symtab, *sym;
427 unsigned char *p, *s, *end;
429 assert(e != NULL);
430 assert(count != NULL);
432 if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
433 LIBELF_SET_ERROR(ARCHIVE, 0);
434 return (NULL);
437 p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
438 end = p + e->e_u.e_ar.e_rawsymtabsz;
440 GET_WORD(p, nentries);
441 p += INTSZ;
443 if (nentries == 0 || p + nentries * INTSZ >= end) {
444 LIBELF_SET_ERROR(ARCHIVE, 0);
445 return (NULL);
448 /* Allocate space for a nentries + a sentinel. */
449 if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
450 LIBELF_SET_ERROR(RESOURCE, 0);
451 return (NULL);
454 s = p + (nentries * INTSZ); /* start of the string table. */
456 for (n = nentries, sym = symtab; n > 0; n--) {
457 off = 0;
459 GET_WORD(p, off);
461 sym->as_off = off;
462 sym->as_hash = elf_hash(s);
463 sym->as_name = (char *)s;
465 p += INTSZ;
466 sym++;
468 for (; s < end && *s++ != '\0';) /* skip to next string */
470 if (s > end) {
471 LIBELF_SET_ERROR(ARCHIVE, 0);
472 free(symtab);
473 return (NULL);
477 /* Fill up the sentinel entry. */
478 sym->as_name = NULL;
479 sym->as_hash = ~0UL;
480 sym->as_off = (off_t) 0;
482 *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
483 e->e_u.e_ar.e_symtab = symtab;
485 return (symtab);