1*0a6a1f1dSLionel Sambuc /* $NetBSD: libelf_ar.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2006,2008,2010 Joseph Koshy
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
30*0a6a1f1dSLionel Sambuc # include "nbtool_config.h"
31*0a6a1f1dSLionel Sambuc #endif
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include <assert.h>
36*0a6a1f1dSLionel Sambuc #include <ctype.h>
37*0a6a1f1dSLionel Sambuc #include <libelf.h>
38*0a6a1f1dSLionel Sambuc #include <stdlib.h>
39*0a6a1f1dSLionel Sambuc #include <string.h>
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuc #include "_libelf.h"
42*0a6a1f1dSLionel Sambuc #include "_libelf_ar.h"
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libelf_ar.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
45*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libelf_ar.c 2225 2011-11-26 18:55:54Z jkoshy ");
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc #define LIBELF_NALLOC_SIZE 16
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc /*
50*0a6a1f1dSLionel Sambuc * `ar' archive handling.
51*0a6a1f1dSLionel Sambuc *
52*0a6a1f1dSLionel Sambuc * `ar' archives start with signature `ARMAG'. Each archive member is
53*0a6a1f1dSLionel Sambuc * preceded by a header containing meta-data for the member. This
54*0a6a1f1dSLionel Sambuc * header is described in <ar.h> (struct ar_hdr). The header always
55*0a6a1f1dSLionel Sambuc * starts on an even address. File data is padded with "\n"
56*0a6a1f1dSLionel Sambuc * characters to keep this invariant.
57*0a6a1f1dSLionel Sambuc *
58*0a6a1f1dSLionel Sambuc * Special considerations for `ar' archives:
59*0a6a1f1dSLionel Sambuc *
60*0a6a1f1dSLionel Sambuc * There are two variants of the `ar' archive format: traditional BSD
61*0a6a1f1dSLionel Sambuc * and SVR4. These differ in the way long file names are treated, and
62*0a6a1f1dSLionel Sambuc * in the layout of the archive symbol table.
63*0a6a1f1dSLionel Sambuc *
64*0a6a1f1dSLionel Sambuc * The `ar' header only has space for a 16 character file name.
65*0a6a1f1dSLionel Sambuc *
66*0a6a1f1dSLionel Sambuc * In the SVR4 format, file names are terminated with a '/', so this
67*0a6a1f1dSLionel Sambuc * effectively leaves 15 characters for the actual file name. Longer
68*0a6a1f1dSLionel Sambuc * file names stored in a separate 'string table' and referenced
69*0a6a1f1dSLionel Sambuc * indirectly from the name field. The string table itself appears as
70*0a6a1f1dSLionel Sambuc * an archive member with name "// ". An `indirect' file name in an
71*0a6a1f1dSLionel Sambuc * `ar' header matches the pattern "/[0-9]*". The digits form a
72*0a6a1f1dSLionel Sambuc * decimal number that corresponds to a byte offset into the string
73*0a6a1f1dSLionel Sambuc * table where the actual file name of the object starts. Strings in
74*0a6a1f1dSLionel Sambuc * the string table are padded to start on even addresses.
75*0a6a1f1dSLionel Sambuc *
76*0a6a1f1dSLionel Sambuc * In the BSD format, file names can be upto 16 characters. File
77*0a6a1f1dSLionel Sambuc * names shorter than 16 characters are padded to 16 characters using
78*0a6a1f1dSLionel Sambuc * (ASCII) space characters. File names with embedded spaces and file
79*0a6a1f1dSLionel Sambuc * names longer than 16 characters are stored immediately after the
80*0a6a1f1dSLionel Sambuc * archive header and the name field set to a special indirect name
81*0a6a1f1dSLionel Sambuc * matching the pattern "#1/[0-9]+". The digits form a decimal number
82*0a6a1f1dSLionel Sambuc * that corresponds to the actual length of the file name following
83*0a6a1f1dSLionel Sambuc * the archive header. The content of the archive member immediately
84*0a6a1f1dSLionel Sambuc * follows the file name, and the size field of the archive member
85*0a6a1f1dSLionel Sambuc * holds the sum of the sizes of the member and of the appended file
86*0a6a1f1dSLionel Sambuc * name.
87*0a6a1f1dSLionel Sambuc *
88*0a6a1f1dSLionel Sambuc * Archives may also have a symbol table (see ranlib(1)), mapping
89*0a6a1f1dSLionel Sambuc * program symbols to object files inside the archive.
90*0a6a1f1dSLionel Sambuc *
91*0a6a1f1dSLionel Sambuc * In the SVR4 format, a symbol table uses a file name of "/ " in its
92*0a6a1f1dSLionel Sambuc * archive header. The symbol table is structured as:
93*0a6a1f1dSLionel Sambuc * - a 4-byte count of entries stored as a binary value, MSB first
94*0a6a1f1dSLionel Sambuc * - 'n' 4-byte offsets, stored as binary values, MSB first
95*0a6a1f1dSLionel Sambuc * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
96*0a6a1f1dSLionel Sambuc *
97*0a6a1f1dSLionel Sambuc * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
98*0a6a1f1dSLionel Sambuc * It is structured as two parts:
99*0a6a1f1dSLionel Sambuc * - The first part is an array of "ranlib" structures preceded by
100*0a6a1f1dSLionel Sambuc * the size of the array in bytes. Each "ranlib" structure
101*0a6a1f1dSLionel Sambuc * describes one symbol. Each structure contains an offset into
102*0a6a1f1dSLionel Sambuc * the string table for the symbol name, and a file offset into the
103*0a6a1f1dSLionel Sambuc * archive for the member defining the symbol.
104*0a6a1f1dSLionel Sambuc * - The second part is a string table containing NUL-terminated
105*0a6a1f1dSLionel Sambuc * strings, preceded by the size of the string table in bytes.
106*0a6a1f1dSLionel Sambuc *
107*0a6a1f1dSLionel Sambuc * If the symbol table and string table are is present in an archive
108*0a6a1f1dSLionel Sambuc * they must be the very first objects and in that order.
109*0a6a1f1dSLionel Sambuc */
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc /*
113*0a6a1f1dSLionel Sambuc * Retrieve an archive header descriptor.
114*0a6a1f1dSLionel Sambuc */
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc Elf_Arhdr *
_libelf_ar_gethdr(Elf * e)117*0a6a1f1dSLionel Sambuc _libelf_ar_gethdr(Elf *e)
118*0a6a1f1dSLionel Sambuc {
119*0a6a1f1dSLionel Sambuc Elf *parent;
120*0a6a1f1dSLionel Sambuc char *namelen;
121*0a6a1f1dSLionel Sambuc Elf_Arhdr *eh;
122*0a6a1f1dSLionel Sambuc size_t n, nlen;
123*0a6a1f1dSLionel Sambuc struct ar_hdr *arh;
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc if ((parent = e->e_parent) == NULL) {
126*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
127*0a6a1f1dSLionel Sambuc return (NULL);
128*0a6a1f1dSLionel Sambuc }
129*0a6a1f1dSLionel Sambuc
130*0a6a1f1dSLionel Sambuc assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
131*0a6a1f1dSLionel Sambuc
132*0a6a1f1dSLionel Sambuc arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
135*0a6a1f1dSLionel Sambuc assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile +
136*0a6a1f1dSLionel Sambuc parent->e_rawsize - sizeof(struct ar_hdr));
137*0a6a1f1dSLionel Sambuc
138*0a6a1f1dSLionel Sambuc if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
139*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, 0);
140*0a6a1f1dSLionel Sambuc return (NULL);
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel Sambuc e->e_hdr.e_arhdr = eh;
144*0a6a1f1dSLionel Sambuc e->e_flags |= LIBELF_F_AR_HEADER;
145*0a6a1f1dSLionel Sambuc
146*0a6a1f1dSLionel Sambuc eh->ar_name = eh->ar_rawname = NULL;
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
149*0a6a1f1dSLionel Sambuc NULL)
150*0a6a1f1dSLionel Sambuc goto error;
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
153*0a6a1f1dSLionel Sambuc &n) == 0)
154*0a6a1f1dSLionel Sambuc goto error;
155*0a6a1f1dSLionel Sambuc eh->ar_uid = (uid_t) n;
156*0a6a1f1dSLionel Sambuc
157*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
158*0a6a1f1dSLionel Sambuc &n) == 0)
159*0a6a1f1dSLionel Sambuc goto error;
160*0a6a1f1dSLionel Sambuc eh->ar_gid = (gid_t) n;
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8,
163*0a6a1f1dSLionel Sambuc &n) == 0)
164*0a6a1f1dSLionel Sambuc goto error;
165*0a6a1f1dSLionel Sambuc eh->ar_mode = (mode_t) n;
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
168*0a6a1f1dSLionel Sambuc &n) == 0)
169*0a6a1f1dSLionel Sambuc goto error;
170*0a6a1f1dSLionel Sambuc
171*0a6a1f1dSLionel Sambuc /*
172*0a6a1f1dSLionel Sambuc * Get the true size of the member if extended naming is being used.
173*0a6a1f1dSLionel Sambuc */
174*0a6a1f1dSLionel Sambuc if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
175*0a6a1f1dSLionel Sambuc namelen = arh->ar_name +
176*0a6a1f1dSLionel Sambuc LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
177*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
178*0a6a1f1dSLionel Sambuc LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
179*0a6a1f1dSLionel Sambuc goto error;
180*0a6a1f1dSLionel Sambuc n -= nlen;
181*0a6a1f1dSLionel Sambuc }
182*0a6a1f1dSLionel Sambuc
183*0a6a1f1dSLionel Sambuc eh->ar_size = n;
184*0a6a1f1dSLionel Sambuc
185*0a6a1f1dSLionel Sambuc if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
186*0a6a1f1dSLionel Sambuc goto error;
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc eh->ar_flags = 0;
189*0a6a1f1dSLionel Sambuc
190*0a6a1f1dSLionel Sambuc return (eh);
191*0a6a1f1dSLionel Sambuc
192*0a6a1f1dSLionel Sambuc error:
193*0a6a1f1dSLionel Sambuc if (eh) {
194*0a6a1f1dSLionel Sambuc if (eh->ar_name)
195*0a6a1f1dSLionel Sambuc free(eh->ar_name);
196*0a6a1f1dSLionel Sambuc if (eh->ar_rawname)
197*0a6a1f1dSLionel Sambuc free(eh->ar_rawname);
198*0a6a1f1dSLionel Sambuc free(eh);
199*0a6a1f1dSLionel Sambuc }
200*0a6a1f1dSLionel Sambuc
201*0a6a1f1dSLionel Sambuc e->e_flags &= ~LIBELF_F_AR_HEADER;
202*0a6a1f1dSLionel Sambuc e->e_hdr.e_rawhdr = (char *) arh;
203*0a6a1f1dSLionel Sambuc
204*0a6a1f1dSLionel Sambuc return (NULL);
205*0a6a1f1dSLionel Sambuc }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc Elf *
_libelf_ar_open_member(int fd,Elf_Cmd c,Elf * elf)208*0a6a1f1dSLionel Sambuc _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
209*0a6a1f1dSLionel Sambuc {
210*0a6a1f1dSLionel Sambuc Elf *e;
211*0a6a1f1dSLionel Sambuc char *member, *namelen;
212*0a6a1f1dSLionel Sambuc size_t nsz, sz;
213*0a6a1f1dSLionel Sambuc off_t next;
214*0a6a1f1dSLionel Sambuc struct ar_hdr *arh;
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambuc assert(elf->e_kind == ELF_K_AR);
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc next = elf->e_u.e_ar.e_next;
219*0a6a1f1dSLionel Sambuc
220*0a6a1f1dSLionel Sambuc /*
221*0a6a1f1dSLionel Sambuc * `next' is only set to zero by elf_next() when the last
222*0a6a1f1dSLionel Sambuc * member of an archive is processed.
223*0a6a1f1dSLionel Sambuc */
224*0a6a1f1dSLionel Sambuc if (next == (off_t) 0)
225*0a6a1f1dSLionel Sambuc return (NULL);
226*0a6a1f1dSLionel Sambuc
227*0a6a1f1dSLionel Sambuc assert((next & 1) == 0);
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc arh = (struct ar_hdr *) (elf->e_rawfile + next);
230*0a6a1f1dSLionel Sambuc
231*0a6a1f1dSLionel Sambuc /*
232*0a6a1f1dSLionel Sambuc * Retrieve the size of the member.
233*0a6a1f1dSLionel Sambuc */
234*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
235*0a6a1f1dSLionel Sambuc &sz) == 0) {
236*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARCHIVE, 0);
237*0a6a1f1dSLionel Sambuc return (NULL);
238*0a6a1f1dSLionel Sambuc }
239*0a6a1f1dSLionel Sambuc
240*0a6a1f1dSLionel Sambuc /*
241*0a6a1f1dSLionel Sambuc * Adjust the size field for members in BSD archives using
242*0a6a1f1dSLionel Sambuc * extended naming.
243*0a6a1f1dSLionel Sambuc */
244*0a6a1f1dSLionel Sambuc if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
245*0a6a1f1dSLionel Sambuc namelen = arh->ar_name +
246*0a6a1f1dSLionel Sambuc LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
247*0a6a1f1dSLionel Sambuc if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
248*0a6a1f1dSLionel Sambuc LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
249*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARCHIVE, 0);
250*0a6a1f1dSLionel Sambuc return (NULL);
251*0a6a1f1dSLionel Sambuc }
252*0a6a1f1dSLionel Sambuc
253*0a6a1f1dSLionel Sambuc member = (char *) (arh + 1) + nsz;
254*0a6a1f1dSLionel Sambuc sz -= nsz;
255*0a6a1f1dSLionel Sambuc } else
256*0a6a1f1dSLionel Sambuc member = (char *) (arh + 1);
257*0a6a1f1dSLionel Sambuc
258*0a6a1f1dSLionel Sambuc
259*0a6a1f1dSLionel Sambuc if ((e = elf_memory((char *) member, sz)) == NULL)
260*0a6a1f1dSLionel Sambuc return (NULL);
261*0a6a1f1dSLionel Sambuc
262*0a6a1f1dSLionel Sambuc e->e_fd = fd;
263*0a6a1f1dSLionel Sambuc e->e_cmd = c;
264*0a6a1f1dSLionel Sambuc e->e_hdr.e_rawhdr = (char *) arh;
265*0a6a1f1dSLionel Sambuc
266*0a6a1f1dSLionel Sambuc elf->e_u.e_ar.e_nchildren++;
267*0a6a1f1dSLionel Sambuc e->e_parent = elf;
268*0a6a1f1dSLionel Sambuc
269*0a6a1f1dSLionel Sambuc return (e);
270*0a6a1f1dSLionel Sambuc }
271*0a6a1f1dSLionel Sambuc
272*0a6a1f1dSLionel Sambuc /*
273*0a6a1f1dSLionel Sambuc * A BSD-style ar(1) symbol table has the following layout:
274*0a6a1f1dSLionel Sambuc *
275*0a6a1f1dSLionel Sambuc * - A count of bytes used by the following array of 'ranlib'
276*0a6a1f1dSLionel Sambuc * structures, stored as a 'long'.
277*0a6a1f1dSLionel Sambuc * - An array of 'ranlib' structures. Each array element is
278*0a6a1f1dSLionel Sambuc * two 'long's in size.
279*0a6a1f1dSLionel Sambuc * - A count of bytes used for the following symbol table.
280*0a6a1f1dSLionel Sambuc * - The symbol table itself.
281*0a6a1f1dSLionel Sambuc */
282*0a6a1f1dSLionel Sambuc
283*0a6a1f1dSLionel Sambuc /*
284*0a6a1f1dSLionel Sambuc * A helper macro to read in a 'long' value from the archive. We use
285*0a6a1f1dSLionel Sambuc * memcpy() since the source pointer may be misaligned with respect to
286*0a6a1f1dSLionel Sambuc * the natural alignment for a C 'long'.
287*0a6a1f1dSLionel Sambuc */
288*0a6a1f1dSLionel Sambuc #define GET_LONG(P, V)do { \
289*0a6a1f1dSLionel Sambuc memcpy(&(V), (P), sizeof(long)); \
290*0a6a1f1dSLionel Sambuc (P) += sizeof(long); \
291*0a6a1f1dSLionel Sambuc } while (/*CONSTCOND*/0)
292*0a6a1f1dSLionel Sambuc
293*0a6a1f1dSLionel Sambuc Elf_Arsym *
_libelf_ar_process_bsd_symtab(Elf * e,size_t * count)294*0a6a1f1dSLionel Sambuc _libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
295*0a6a1f1dSLionel Sambuc {
296*0a6a1f1dSLionel Sambuc Elf_Arsym *symtab, *sym;
297*0a6a1f1dSLionel Sambuc unsigned char *end, *p, *p0, *s, *s0;
298*0a6a1f1dSLionel Sambuc const unsigned int entrysize = 2 * sizeof(long);
299*0a6a1f1dSLionel Sambuc long arraysize, fileoffset, n, nentries, stroffset, strtabsize;
300*0a6a1f1dSLionel Sambuc
301*0a6a1f1dSLionel Sambuc assert(e != NULL);
302*0a6a1f1dSLionel Sambuc assert(count != NULL);
303*0a6a1f1dSLionel Sambuc assert(e->e_u.e_ar.e_symtab == NULL);
304*0a6a1f1dSLionel Sambuc
305*0a6a1f1dSLionel Sambuc symtab = NULL;
306*0a6a1f1dSLionel Sambuc
307*0a6a1f1dSLionel Sambuc /*
308*0a6a1f1dSLionel Sambuc * The BSD symbol table always contains the count fields even
309*0a6a1f1dSLionel Sambuc * if there are no entries in it.
310*0a6a1f1dSLionel Sambuc */
311*0a6a1f1dSLionel Sambuc if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
312*0a6a1f1dSLionel Sambuc goto symtaberror;
313*0a6a1f1dSLionel Sambuc
314*0a6a1f1dSLionel Sambuc p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
315*0a6a1f1dSLionel Sambuc end = p0 + e->e_u.e_ar.e_rawsymtabsz;
316*0a6a1f1dSLionel Sambuc
317*0a6a1f1dSLionel Sambuc /*
318*0a6a1f1dSLionel Sambuc * Retrieve the size of the array of ranlib descriptors and
319*0a6a1f1dSLionel Sambuc * check it for validity.
320*0a6a1f1dSLionel Sambuc */
321*0a6a1f1dSLionel Sambuc GET_LONG(p, arraysize);
322*0a6a1f1dSLionel Sambuc
323*0a6a1f1dSLionel Sambuc if (p0 + arraysize >= end || (arraysize % entrysize != 0))
324*0a6a1f1dSLionel Sambuc goto symtaberror;
325*0a6a1f1dSLionel Sambuc
326*0a6a1f1dSLionel Sambuc /*
327*0a6a1f1dSLionel Sambuc * Check the value of the string table size.
328*0a6a1f1dSLionel Sambuc */
329*0a6a1f1dSLionel Sambuc s = p + arraysize;
330*0a6a1f1dSLionel Sambuc GET_LONG(s, strtabsize);
331*0a6a1f1dSLionel Sambuc
332*0a6a1f1dSLionel Sambuc s0 = s; /* Start of string table. */
333*0a6a1f1dSLionel Sambuc if (s0 + strtabsize > end)
334*0a6a1f1dSLionel Sambuc goto symtaberror;
335*0a6a1f1dSLionel Sambuc
336*0a6a1f1dSLionel Sambuc nentries = arraysize / entrysize;
337*0a6a1f1dSLionel Sambuc
338*0a6a1f1dSLionel Sambuc /*
339*0a6a1f1dSLionel Sambuc * Allocate space for the returned Elf_Arsym array.
340*0a6a1f1dSLionel Sambuc */
341*0a6a1f1dSLionel Sambuc if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
342*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, 0);
343*0a6a1f1dSLionel Sambuc return (NULL);
344*0a6a1f1dSLionel Sambuc }
345*0a6a1f1dSLionel Sambuc
346*0a6a1f1dSLionel Sambuc /* Read in symbol table entries. */
347*0a6a1f1dSLionel Sambuc for (n = 0, sym = symtab; n < nentries; n++, sym++) {
348*0a6a1f1dSLionel Sambuc GET_LONG(p, stroffset);
349*0a6a1f1dSLionel Sambuc GET_LONG(p, fileoffset);
350*0a6a1f1dSLionel Sambuc
351*0a6a1f1dSLionel Sambuc s = s0 + stroffset;
352*0a6a1f1dSLionel Sambuc
353*0a6a1f1dSLionel Sambuc if (s >= end)
354*0a6a1f1dSLionel Sambuc goto symtaberror;
355*0a6a1f1dSLionel Sambuc
356*0a6a1f1dSLionel Sambuc sym->as_off = fileoffset;
357*0a6a1f1dSLionel Sambuc sym->as_hash = elf_hash((char *) s);
358*0a6a1f1dSLionel Sambuc sym->as_name = (char *) s;
359*0a6a1f1dSLionel Sambuc }
360*0a6a1f1dSLionel Sambuc
361*0a6a1f1dSLionel Sambuc /* Fill up the sentinel entry. */
362*0a6a1f1dSLionel Sambuc sym->as_name = NULL;
363*0a6a1f1dSLionel Sambuc sym->as_hash = ~0UL;
364*0a6a1f1dSLionel Sambuc sym->as_off = (off_t) 0;
365*0a6a1f1dSLionel Sambuc
366*0a6a1f1dSLionel Sambuc /* Remember the processed symbol table. */
367*0a6a1f1dSLionel Sambuc e->e_u.e_ar.e_symtab = symtab;
368*0a6a1f1dSLionel Sambuc
369*0a6a1f1dSLionel Sambuc *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
370*0a6a1f1dSLionel Sambuc
371*0a6a1f1dSLionel Sambuc return (symtab);
372*0a6a1f1dSLionel Sambuc
373*0a6a1f1dSLionel Sambuc symtaberror:
374*0a6a1f1dSLionel Sambuc if (symtab)
375*0a6a1f1dSLionel Sambuc free(symtab);
376*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARCHIVE, 0);
377*0a6a1f1dSLionel Sambuc return (NULL);
378*0a6a1f1dSLionel Sambuc }
379*0a6a1f1dSLionel Sambuc
380*0a6a1f1dSLionel Sambuc /*
381*0a6a1f1dSLionel Sambuc * An SVR4-style ar(1) symbol table has the following layout:
382*0a6a1f1dSLionel Sambuc *
383*0a6a1f1dSLionel Sambuc * - The first 4 bytes are a binary count of the number of entries in the
384*0a6a1f1dSLionel Sambuc * symbol table, stored MSB-first.
385*0a6a1f1dSLionel Sambuc * - Then there are 'n' 4-byte binary offsets, also stored MSB first.
386*0a6a1f1dSLionel Sambuc * - Following this, there are 'n' null-terminated strings.
387*0a6a1f1dSLionel Sambuc */
388*0a6a1f1dSLionel Sambuc
389*0a6a1f1dSLionel Sambuc #define GET_WORD(P, V) do { \
390*0a6a1f1dSLionel Sambuc (V) = 0; \
391*0a6a1f1dSLionel Sambuc (V) = (P)[0]; (V) <<= 8; \
392*0a6a1f1dSLionel Sambuc (V) += (P)[1]; (V) <<= 8; \
393*0a6a1f1dSLionel Sambuc (V) += (P)[2]; (V) <<= 8; \
394*0a6a1f1dSLionel Sambuc (V) += (P)[3]; \
395*0a6a1f1dSLionel Sambuc } while (0)
396*0a6a1f1dSLionel Sambuc
397*0a6a1f1dSLionel Sambuc #define INTSZ 4
398*0a6a1f1dSLionel Sambuc
399*0a6a1f1dSLionel Sambuc
400*0a6a1f1dSLionel Sambuc Elf_Arsym *
_libelf_ar_process_svr4_symtab(Elf * e,size_t * count)401*0a6a1f1dSLionel Sambuc _libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
402*0a6a1f1dSLionel Sambuc {
403*0a6a1f1dSLionel Sambuc size_t n, nentries, off;
404*0a6a1f1dSLionel Sambuc Elf_Arsym *symtab, *sym;
405*0a6a1f1dSLionel Sambuc unsigned char *p, *s, *end;
406*0a6a1f1dSLionel Sambuc
407*0a6a1f1dSLionel Sambuc assert(e != NULL);
408*0a6a1f1dSLionel Sambuc assert(count != NULL);
409*0a6a1f1dSLionel Sambuc assert(e->e_u.e_ar.e_symtab == NULL);
410*0a6a1f1dSLionel Sambuc
411*0a6a1f1dSLionel Sambuc symtab = NULL;
412*0a6a1f1dSLionel Sambuc
413*0a6a1f1dSLionel Sambuc if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
414*0a6a1f1dSLionel Sambuc goto symtaberror;
415*0a6a1f1dSLionel Sambuc
416*0a6a1f1dSLionel Sambuc p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
417*0a6a1f1dSLionel Sambuc end = p + e->e_u.e_ar.e_rawsymtabsz;
418*0a6a1f1dSLionel Sambuc
419*0a6a1f1dSLionel Sambuc GET_WORD(p, nentries);
420*0a6a1f1dSLionel Sambuc p += INTSZ;
421*0a6a1f1dSLionel Sambuc
422*0a6a1f1dSLionel Sambuc if (nentries == 0 || p + nentries * INTSZ >= end)
423*0a6a1f1dSLionel Sambuc goto symtaberror;
424*0a6a1f1dSLionel Sambuc
425*0a6a1f1dSLionel Sambuc /* Allocate space for a nentries + a sentinel. */
426*0a6a1f1dSLionel Sambuc if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
427*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, 0);
428*0a6a1f1dSLionel Sambuc return (NULL);
429*0a6a1f1dSLionel Sambuc }
430*0a6a1f1dSLionel Sambuc
431*0a6a1f1dSLionel Sambuc s = p + (nentries * INTSZ); /* start of the string table. */
432*0a6a1f1dSLionel Sambuc
433*0a6a1f1dSLionel Sambuc for (n = nentries, sym = symtab; n > 0; n--) {
434*0a6a1f1dSLionel Sambuc
435*0a6a1f1dSLionel Sambuc if (s >= end)
436*0a6a1f1dSLionel Sambuc goto symtaberror;
437*0a6a1f1dSLionel Sambuc
438*0a6a1f1dSLionel Sambuc off = 0;
439*0a6a1f1dSLionel Sambuc
440*0a6a1f1dSLionel Sambuc GET_WORD(p, off);
441*0a6a1f1dSLionel Sambuc
442*0a6a1f1dSLionel Sambuc sym->as_off = off;
443*0a6a1f1dSLionel Sambuc sym->as_hash = elf_hash((char *) s);
444*0a6a1f1dSLionel Sambuc sym->as_name = (char *) s;
445*0a6a1f1dSLionel Sambuc
446*0a6a1f1dSLionel Sambuc p += INTSZ;
447*0a6a1f1dSLionel Sambuc sym++;
448*0a6a1f1dSLionel Sambuc
449*0a6a1f1dSLionel Sambuc for (; s < end && *s++ != '\0';) /* skip to next string */
450*0a6a1f1dSLionel Sambuc ;
451*0a6a1f1dSLionel Sambuc }
452*0a6a1f1dSLionel Sambuc
453*0a6a1f1dSLionel Sambuc /* Fill up the sentinel entry. */
454*0a6a1f1dSLionel Sambuc sym->as_name = NULL;
455*0a6a1f1dSLionel Sambuc sym->as_hash = ~0UL;
456*0a6a1f1dSLionel Sambuc sym->as_off = (off_t) 0;
457*0a6a1f1dSLionel Sambuc
458*0a6a1f1dSLionel Sambuc *count = e->e_u.e_ar.e_symtabsz = nentries + 1;
459*0a6a1f1dSLionel Sambuc e->e_u.e_ar.e_symtab = symtab;
460*0a6a1f1dSLionel Sambuc
461*0a6a1f1dSLionel Sambuc return (symtab);
462*0a6a1f1dSLionel Sambuc
463*0a6a1f1dSLionel Sambuc symtaberror:
464*0a6a1f1dSLionel Sambuc if (symtab)
465*0a6a1f1dSLionel Sambuc free(symtab);
466*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARCHIVE, 0);
467*0a6a1f1dSLionel Sambuc return (NULL);
468*0a6a1f1dSLionel Sambuc }
469