Remove building with NOCRYPTO option
[minix.git] / external / bsd / elftoolchain / dist / libelf / libelf_ar.c
blobcfb77bf56cf2b4b5a854fccf8139188604f3597a
1 /* $NetBSD: libelf_ar.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2006,2008,2010 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 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
33 #include <sys/cdefs.h>
35 #include <assert.h>
36 #include <ctype.h>
37 #include <libelf.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "_libelf.h"
42 #include "_libelf_ar.h"
44 __RCSID("$NetBSD: libelf_ar.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
45 ELFTC_VCSID("Id: libelf_ar.c 2225 2011-11-26 18:55:54Z jkoshy ");
47 #define LIBELF_NALLOC_SIZE 16
50 * `ar' archive handling.
52 * `ar' archives start with signature `ARMAG'. Each archive member is
53 * preceded by a header containing meta-data for the member. This
54 * header is described in <ar.h> (struct ar_hdr). The header always
55 * starts on an even address. File data is padded with "\n"
56 * characters to keep this invariant.
58 * Special considerations for `ar' archives:
60 * There are two variants of the `ar' archive format: traditional BSD
61 * and SVR4. These differ in the way long file names are treated, and
62 * in the layout of the archive symbol table.
64 * The `ar' header only has space for a 16 character file name.
66 * In the SVR4 format, file names are terminated with a '/', so this
67 * effectively leaves 15 characters for the actual file name. Longer
68 * file names stored in a separate 'string table' and referenced
69 * indirectly from the name field. The string table itself appears as
70 * an archive member with name "// ". An `indirect' file name in an
71 * `ar' header matches the pattern "/[0-9]*". The digits form a
72 * decimal number that corresponds to a byte offset into the string
73 * table where the actual file name of the object starts. Strings in
74 * the string table are padded to start on even addresses.
76 * In the BSD format, file names can be upto 16 characters. File
77 * names shorter than 16 characters are padded to 16 characters using
78 * (ASCII) space characters. File names with embedded spaces and file
79 * names longer than 16 characters are stored immediately after the
80 * archive header and the name field set to a special indirect name
81 * matching the pattern "#1/[0-9]+". The digits form a decimal number
82 * that corresponds to the actual length of the file name following
83 * the archive header. The content of the archive member immediately
84 * follows the file name, and the size field of the archive member
85 * holds the sum of the sizes of the member and of the appended file
86 * name.
88 * Archives may also have a symbol table (see ranlib(1)), mapping
89 * program symbols to object files inside the archive.
91 * In the SVR4 format, a symbol table uses a file name of "/ " in its
92 * archive header. The symbol table is structured as:
93 * - a 4-byte count of entries stored as a binary value, MSB first
94 * - 'n' 4-byte offsets, stored as binary values, MSB first
95 * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
97 * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
98 * It is structured as two parts:
99 * - The first part is an array of "ranlib" structures preceded by
100 * the size of the array in bytes. Each "ranlib" structure
101 * describes one symbol. Each structure contains an offset into
102 * the string table for the symbol name, and a file offset into the
103 * archive for the member defining the symbol.
104 * - The second part is a string table containing NUL-terminated
105 * strings, preceded by the size of the string table in bytes.
107 * If the symbol table and string table are is present in an archive
108 * they must be the very first objects and in that order.
113 * Retrieve an archive header descriptor.
116 Elf_Arhdr *
117 _libelf_ar_gethdr(Elf *e)
119 Elf *parent;
120 char *namelen;
121 Elf_Arhdr *eh;
122 size_t n, nlen;
123 struct ar_hdr *arh;
125 if ((parent = e->e_parent) == NULL) {
126 LIBELF_SET_ERROR(ARGUMENT, 0);
127 return (NULL);
130 assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
132 arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
134 assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
135 assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile +
136 parent->e_rawsize - sizeof(struct ar_hdr));
138 if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
139 LIBELF_SET_ERROR(RESOURCE, 0);
140 return (NULL);
143 e->e_hdr.e_arhdr = eh;
144 e->e_flags |= LIBELF_F_AR_HEADER;
146 eh->ar_name = eh->ar_rawname = NULL;
148 if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
149 NULL)
150 goto error;
152 if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
153 &n) == 0)
154 goto error;
155 eh->ar_uid = (uid_t) n;
157 if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
158 &n) == 0)
159 goto error;
160 eh->ar_gid = (gid_t) n;
162 if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8,
163 &n) == 0)
164 goto error;
165 eh->ar_mode = (mode_t) n;
167 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
168 &n) == 0)
169 goto error;
172 * Get the true size of the member if extended naming is being used.
174 if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
175 namelen = arh->ar_name +
176 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
177 if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
178 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
179 goto error;
180 n -= nlen;
183 eh->ar_size = n;
185 if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
186 goto error;
188 eh->ar_flags = 0;
190 return (eh);
192 error:
193 if (eh) {
194 if (eh->ar_name)
195 free(eh->ar_name);
196 if (eh->ar_rawname)
197 free(eh->ar_rawname);
198 free(eh);
201 e->e_flags &= ~LIBELF_F_AR_HEADER;
202 e->e_hdr.e_rawhdr = (char *) arh;
204 return (NULL);
207 Elf *
208 _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
210 Elf *e;
211 char *member, *namelen;
212 size_t nsz, sz;
213 off_t next;
214 struct ar_hdr *arh;
216 assert(elf->e_kind == ELF_K_AR);
218 next = elf->e_u.e_ar.e_next;
221 * `next' is only set to zero by elf_next() when the last
222 * member of an archive is processed.
224 if (next == (off_t) 0)
225 return (NULL);
227 assert((next & 1) == 0);
229 arh = (struct ar_hdr *) (elf->e_rawfile + next);
232 * Retrieve the size of the member.
234 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
235 &sz) == 0) {
236 LIBELF_SET_ERROR(ARCHIVE, 0);
237 return (NULL);
241 * Adjust the size field for members in BSD archives using
242 * extended naming.
244 if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
245 namelen = arh->ar_name +
246 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
247 if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
248 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
249 LIBELF_SET_ERROR(ARCHIVE, 0);
250 return (NULL);
253 member = (char *) (arh + 1) + nsz;
254 sz -= nsz;
255 } else
256 member = (char *) (arh + 1);
259 if ((e = elf_memory((char *) member, sz)) == NULL)
260 return (NULL);
262 e->e_fd = fd;
263 e->e_cmd = c;
264 e->e_hdr.e_rawhdr = (char *) arh;
266 elf->e_u.e_ar.e_nchildren++;
267 e->e_parent = elf;
269 return (e);
273 * A BSD-style ar(1) symbol table has the following layout:
275 * - A count of bytes used by the following array of 'ranlib'
276 * structures, stored as a 'long'.
277 * - An array of 'ranlib' structures. Each array element is
278 * two 'long's in size.
279 * - A count of bytes used for the following symbol table.
280 * - The symbol table itself.
284 * A helper macro to read in a 'long' value from the archive. We use
285 * memcpy() since the source pointer may be misaligned with respect to
286 * the natural alignment for a C 'long'.
288 #define GET_LONG(P, V)do { \
289 memcpy(&(V), (P), sizeof(long)); \
290 (P) += sizeof(long); \
291 } while (/*CONSTCOND*/0)
293 Elf_Arsym *
294 _libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
296 Elf_Arsym *symtab, *sym;
297 unsigned char *end, *p, *p0, *s, *s0;
298 const unsigned int entrysize = 2 * sizeof(long);
299 long arraysize, fileoffset, n, nentries, stroffset, strtabsize;
301 assert(e != NULL);
302 assert(count != NULL);
303 assert(e->e_u.e_ar.e_symtab == NULL);
305 symtab = NULL;
308 * The BSD symbol table always contains the count fields even
309 * if there are no entries in it.
311 if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
312 goto symtaberror;
314 p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
315 end = p0 + e->e_u.e_ar.e_rawsymtabsz;
318 * Retrieve the size of the array of ranlib descriptors and
319 * check it for validity.
321 GET_LONG(p, arraysize);
323 if (p0 + arraysize >= end || (arraysize % entrysize != 0))
324 goto symtaberror;
327 * Check the value of the string table size.
329 s = p + arraysize;
330 GET_LONG(s, strtabsize);
332 s0 = s; /* Start of string table. */
333 if (s0 + strtabsize > end)
334 goto symtaberror;
336 nentries = arraysize / entrysize;
339 * Allocate space for the returned Elf_Arsym array.
341 if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
342 LIBELF_SET_ERROR(RESOURCE, 0);
343 return (NULL);
346 /* Read in symbol table entries. */
347 for (n = 0, sym = symtab; n < nentries; n++, sym++) {
348 GET_LONG(p, stroffset);
349 GET_LONG(p, fileoffset);
351 s = s0 + stroffset;
353 if (s >= end)
354 goto symtaberror;
356 sym->as_off = fileoffset;
357 sym->as_hash = elf_hash((char *) s);
358 sym->as_name = (char *) s;
361 /* Fill up the sentinel entry. */
362 sym->as_name = NULL;
363 sym->as_hash = ~0UL;
364 sym->as_off = (off_t) 0;
366 /* Remember the processed symbol table. */
367 e->e_u.e_ar.e_symtab = symtab;
369 *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
371 return (symtab);
373 symtaberror:
374 if (symtab)
375 free(symtab);
376 LIBELF_SET_ERROR(ARCHIVE, 0);
377 return (NULL);
381 * An SVR4-style ar(1) symbol table has the following layout:
383 * - The first 4 bytes are a binary count of the number of entries in the
384 * symbol table, stored MSB-first.
385 * - Then there are 'n' 4-byte binary offsets, also stored MSB first.
386 * - Following this, there are 'n' null-terminated strings.
389 #define GET_WORD(P, V) do { \
390 (V) = 0; \
391 (V) = (P)[0]; (V) <<= 8; \
392 (V) += (P)[1]; (V) <<= 8; \
393 (V) += (P)[2]; (V) <<= 8; \
394 (V) += (P)[3]; \
395 } while (0)
397 #define INTSZ 4
400 Elf_Arsym *
401 _libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
403 size_t n, nentries, off;
404 Elf_Arsym *symtab, *sym;
405 unsigned char *p, *s, *end;
407 assert(e != NULL);
408 assert(count != NULL);
409 assert(e->e_u.e_ar.e_symtab == NULL);
411 symtab = NULL;
413 if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
414 goto symtaberror;
416 p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
417 end = p + e->e_u.e_ar.e_rawsymtabsz;
419 GET_WORD(p, nentries);
420 p += INTSZ;
422 if (nentries == 0 || p + nentries * INTSZ >= end)
423 goto symtaberror;
425 /* Allocate space for a nentries + a sentinel. */
426 if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
427 LIBELF_SET_ERROR(RESOURCE, 0);
428 return (NULL);
431 s = p + (nentries * INTSZ); /* start of the string table. */
433 for (n = nentries, sym = symtab; n > 0; n--) {
435 if (s >= end)
436 goto symtaberror;
438 off = 0;
440 GET_WORD(p, off);
442 sym->as_off = off;
443 sym->as_hash = elf_hash((char *) s);
444 sym->as_name = (char *) s;
446 p += INTSZ;
447 sym++;
449 for (; s < end && *s++ != '\0';) /* skip to next string */
453 /* Fill up the sentinel entry. */
454 sym->as_name = NULL;
455 sym->as_hash = ~0UL;
456 sym->as_off = (off_t) 0;
458 *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
459 e->e_u.e_ar.e_symtab = symtab;
461 return (symtab);
463 symtaberror:
464 if (symtab)
465 free(symtab);
466 LIBELF_SET_ERROR(ARCHIVE, 0);
467 return (NULL);