1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 *	Copyright (c) 1988 AT&T
29 *	  All Rights Reserved
30 */
31
32#pragma ident	"@(#)ar.c	1.24	08/05/31 SMI"
33
34#include <ar.h>
35#include <stdlib.h>
36#include <memory.h>
37#include <errno.h>
38#include <libelf.h>
39#include "decl.h"
40#include "msg.h"
41#include "member.h"
42#include <string.h>
43
44#define	MANGLE	'\177'
45
46
47/*
48 * Archive processing
49 *	When processing an archive member, two things can happen
50 *	that are a little tricky.
51 *
52 * Sliding
53 *	Sliding support is left in for backward compatibility and for
54 *	support of Archives produced on other systems.  The bundled
55 *	ar(1) produces archives with all members on a 4 byte boundry,
56 *	so current archives should need no sliding.
57 *
58 *	Archive members that are only 2-byte aligned within the file will
59 *	be slid.  To reuse the file's memory image, the library slides an
60 *	archive member into its header to align the bytes.  This means
61 *	the header must be disposable.
62 *
63 * Header reuse
64 *	Because the library can trample the header, it must be preserved to
65 *	avoid restrictions on archive member reuse.  That is, if the member
66 *	header changes, the library may see garbage the next time it looks
67 *	at the header.  After extracting the original header, the library
68 *	appends it to the parents `ed_memlist' list, thus future lookups first
69 *	check this list to determine if a member has previously been processed
70 *	and whether sliding occured.
71 */
72
73
74/*
75 * Size check
76 *	If the header is too small, the following generates a negative
77 *	subscript for x.x and fails to compile.
78 *
79 * The check is based on sizeof (Elf64) because that's always going
80 * to be at least as big as Elf32.
81 */
82
83struct	x
84{
85	char	x[sizeof (struct ar_hdr) - 3 * sizeof (Elf64) - 1];
86};
87
88
89
90static const char	fmag[] = ARFMAG;
91
92
93/*
94 * Convert a string starting at 'p' and ending at 'end' into
95 * an integer.  Base is the base of the number being converted
96 * (either 8 or 10).
97 *
98 * Returns the converted integer of the string being scaned.
99 */
100unsigned long
101_elf_number(char * p, char * end, int base)
102{
103	register unsigned	c;
104	register unsigned long	n = 0;
105
106	while (p < end) {
107		if ((c = *p - '0') >= base) {
108			while (*p++ == ' ')
109				if (p >= end)
110					return (n);
111			return (0);
112		}
113		n *= base;
114		n += c;
115		++p;
116	}
117	return (n);
118}
119
120
121/*
122 * Convert ar_hdr to Member
123 *	Converts ascii file representation to the binary memory values.
124 */
125Member *
126_elf_armem(Elf * elf, char * file, size_t fsz)
127{
128	register struct ar_hdr	*f = (struct ar_hdr *)file;
129	register Member		*m;
130	register Memlist	*l, * ol;
131	register Memident	*i;
132
133	if (fsz < sizeof (struct ar_hdr)) {
134		_elf_seterr(EFMT_ARHDRSZ, 0);
135		return (0);
136	}
137
138	/*
139	 * Determine in this member has already been processed
140	 */
141	for (l = elf->ed_memlist, ol = l; l; ol = l, l = l->m_next)
142		for (i = (Memident *)(l + 1); i < l->m_free; i++)
143			if (i->m_offset == file)
144				return (i->m_member);
145
146	if (f->ar_fmag[0] != fmag[0] || f->ar_fmag[1] != fmag[1]) {
147		_elf_seterr(EFMT_ARFMAG, 0);
148		return (0);
149	}
150
151	/*
152	 * Allocate a new member structure and assign it to the next free
153	 * free memlist ident.
154	 */
155	if ((m = (Member *)malloc(sizeof (Member))) == 0) {
156		_elf_seterr(EMEM_ARMEM, errno);
157		return (0);
158	}
159	if ((elf->ed_memlist == 0) || (ol->m_free == ol->m_end)) {
160		if ((l = (Memlist *)malloc(sizeof (Memlist) +
161		    (sizeof (Memident) * MEMIDENTNO))) == 0) {
162			_elf_seterr(EMEM_ARMEM, errno);
163			return (0);
164		}
165		l->m_next = 0;
166		l->m_free = (Memident *)(l + 1);
167		l->m_end = (Memident *)((uintptr_t)l->m_free +
168			(sizeof (Memident) * MEMIDENTNO));
169
170		if (elf->ed_memlist == 0)
171			elf->ed_memlist = l;
172		else
173			ol->m_next = l;
174		ol = l;
175	}
176	ol->m_free->m_offset = file;
177	ol->m_free->m_member = m;
178	ol->m_free++;
179
180	m->m_err = 0;
181	(void) memcpy(m->m_name, f->ar_name, ARSZ(ar_name));
182	m->m_name[ARSZ(ar_name)] = '\0';
183	m->m_hdr.ar_name = m->m_name;
184	(void) memcpy(m->m_raw, f->ar_name, ARSZ(ar_name));
185	m->m_raw[ARSZ(ar_name)] = '\0';
186	m->m_hdr.ar_rawname = m->m_raw;
187	m->m_slide = 0;
188
189	/*
190	 * Classify file name.
191	 * If a name error occurs, delay until getarhdr().
192	 */
193
194	if (f->ar_name[0] != '/') {	/* regular name */
195		register char	*p;
196
197		p = &m->m_name[sizeof (m->m_name)];
198		while (*--p != '/')
199			if (p <= m->m_name)
200				break;
201		*p = '\0';
202	} else if (f->ar_name[1] >= '0' && f->ar_name[1] <= '9') { /* strtab */
203		register unsigned long	j;
204
205		j = _elf_number(&f->ar_name[1],
206			&f->ar_name[ARSZ(ar_name)], 10);
207		if (j < elf->ed_arstrsz)
208			m->m_hdr.ar_name = elf->ed_arstr + j;
209		else {
210			m->m_hdr.ar_name = 0;
211			/*LINTED*/ /* MSG_INTL(EFMT_ARSTRNM) */
212			m->m_err = (int)EFMT_ARSTRNM;
213		}
214	} else if (f->ar_name[1] == ' ')			/* "/" */
215		m->m_name[1] = '\0';
216	else if (f->ar_name[1] == '/' && f->ar_name[2] == ' ')	/* "//" */
217		m->m_name[2] = '\0';
218	else {							/* "/?" */
219		m->m_hdr.ar_name = 0;
220		/*LINTED*/ /* MSG_INTL(EFMT_ARUNKNM) */
221		m->m_err = (int)EFMT_ARUNKNM;
222	}
223
224	m->m_hdr.ar_date = (time_t)_elf_number(f->ar_date,
225		&f->ar_date[ARSZ(ar_date)], 10);
226	/* LINTED */
227	m->m_hdr.ar_uid = (uid_t)_elf_number(f->ar_uid,
228		&f->ar_uid[ARSZ(ar_uid)], 10);
229	/* LINTED */
230	m->m_hdr.ar_gid = (gid_t)_elf_number(f->ar_gid,
231		&f->ar_gid[ARSZ(ar_gid)], 10);
232	/* LINTED */
233	m->m_hdr.ar_mode = (mode_t)_elf_number(f->ar_mode,
234		&f->ar_mode[ARSZ(ar_mode)], 8);
235	m->m_hdr.ar_size = (off_t)_elf_number(f->ar_size,
236		&f->ar_size[ARSZ(ar_size)], 10);
237
238	return (m);
239}
240
241
242/*
243 * Initial archive processing
244 *	An archive may have two special members.
245 *	A symbol table, named /, must be first if it is present.
246 *	A string table, named //, must precede all "normal" members.
247 *
248 *	This code "peeks" at headers but doesn't change them.
249 *	Later processing wants original headers.
250 *
251 *	String table is converted, changing '/' name terminators
252 *	to nulls.  The last byte in the string table, which should
253 *	be '\n', is set to nil, guaranteeing null termination.  That
254 *	byte should be '\n', but this code doesn't check.
255 *
256 *	The symbol table conversion is delayed until needed.
257 */
258void
259_elf_arinit(Elf * elf)
260{
261	char *				base = elf->ed_ident;
262	register char *			end = base + elf->ed_fsz;
263	register struct ar_hdr *	a;
264	register char *			hdr = base + SARMAG;
265	register char *			mem;
266	int				j;
267	size_t				sz = SARMAG;
268
269	elf->ed_status = ES_COOKED;
270	elf->ed_nextoff = SARMAG;
271	for (j = 0; j < 2; ++j)	 {	/* 2 special members */
272		unsigned long	n;
273
274		if (((end - hdr) < sizeof (struct ar_hdr)) ||
275		    (_elf_vm(elf, (size_t)(SARMAG),
276		    sizeof (struct ar_hdr)) != OK_YES))
277			return;
278
279		a = (struct ar_hdr *)hdr;
280		mem = (char *)a + sizeof (struct ar_hdr);
281		n = _elf_number(a->ar_size, &a->ar_size[ARSZ(ar_size)], 10);
282		if ((end - mem < n) || (a->ar_name[0] != '/') ||
283		    ((sz = n) != n)) {
284			return;
285		}
286
287		hdr = mem + sz;
288		if (a->ar_name[1] == ' ') {
289			elf->ed_arsym = mem;
290			elf->ed_arsymsz = sz;
291			elf->ed_arsymoff = (char *)a - base;
292		} else if (a->ar_name[1] == '/' && a->ar_name[2] == ' ') {
293			int	k;
294
295			if (_elf_vm(elf, (size_t)(mem - elf->ed_ident),
296			    sz) != OK_YES)
297				return;
298			if (elf->ed_vm == 0) {
299				char *	nmem;
300				if ((nmem = malloc(sz)) == 0) {
301					_elf_seterr(EMEM_ARSTR, errno);
302					return;
303				}
304				(void) memcpy(nmem, mem, sz);
305				elf->ed_myflags |= EDF_ASTRALLOC;
306				mem = nmem;
307			}
308
309			elf->ed_arstr = mem;
310			elf->ed_arstrsz = sz;
311			elf->ed_arstroff = (char *)a - base;
312			for (k = 0; k < sz; k++) {
313				if (*mem == '/')
314					*mem = '\0';
315				++mem;
316			}
317			*(mem - 1) = '\0';
318		} else {
319			return;
320		}
321		hdr += sz & 1;
322	}
323}
324