xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getarsym.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.6	*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma weak	elf_getarsym = _elf_getarsym
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "syn.h"
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <libelf.h>
41*0Sstevel@tonic-gate #include "decl.h"
42*0Sstevel@tonic-gate #include "msg.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * Convert archive symbol table to memory format
47*0Sstevel@tonic-gate  *	This takes a pointer to file's archive symbol table,
48*0Sstevel@tonic-gate  *	alignment unconstrained.  Returns null terminated
49*0Sstevel@tonic-gate  *	vector of Elf_Arsym structures.
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  *	Symbol table is the following:
52*0Sstevel@tonic-gate  *		# offsets	4-byte word
53*0Sstevel@tonic-gate  *		offset[0...]	4-byte word each
54*0Sstevel@tonic-gate  *		strings		null-terminated, for offset[x]
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define	get4(p)	((((((p[0]<<8)+p[1])<<8)+p[2])<<8)+p[3])
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static Elf_Void	*arsym	_((Byte *, size_t, size_t *));
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate Elf_Void *
65*0Sstevel@tonic-gate arsym(Byte * off, size_t sz, size_t * e)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	char		*endstr = (char *)off + sz;
68*0Sstevel@tonic-gate 	register char	*str;
69*0Sstevel@tonic-gate 	Byte		*endoff;
70*0Sstevel@tonic-gate 	Elf_Void	*oas;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	{
73*0Sstevel@tonic-gate 		register size_t	n;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 		if (sz < 4 || (sz - 4) / 4 < (n = get4(off))) {
76*0Sstevel@tonic-gate 			_elf_seterr(EFMT_ARSYMSZ, 0);
77*0Sstevel@tonic-gate 			return (0);
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 		off += 4;
80*0Sstevel@tonic-gate 		endoff = off + n * 4;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 		/*
83*0Sstevel@tonic-gate 		 * string table must be present, null terminated
84*0Sstevel@tonic-gate 		 */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		if (((str = (char *)endoff) >= endstr) ||
87*0Sstevel@tonic-gate 		    (*(endstr - 1) != '\0')) {
88*0Sstevel@tonic-gate 			_elf_seterr(EFMT_ARSYM, 0);
89*0Sstevel@tonic-gate 			return (0);
90*0Sstevel@tonic-gate 		}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 		/*
93*0Sstevel@tonic-gate 		 * overflow can occur here, but not likely
94*0Sstevel@tonic-gate 		 */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		*e = n + 1;
97*0Sstevel@tonic-gate 		n = sizeof (Elf_Arsym) * (n + 1);
98*0Sstevel@tonic-gate 		if ((oas = malloc(n)) == 0) {
99*0Sstevel@tonic-gate 			_elf_seterr(EMEM_ARSYM, errno);
100*0Sstevel@tonic-gate 			return (0);
101*0Sstevel@tonic-gate 		}
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	{
104*0Sstevel@tonic-gate 		register Elf_Arsym	*as = (Elf_Arsym *)oas;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 		while (off < endoff) {
107*0Sstevel@tonic-gate 			if (str >= endstr) {
108*0Sstevel@tonic-gate 				_elf_seterr(EFMT_ARSYMSTR, 0);
109*0Sstevel@tonic-gate 				free(oas);
110*0Sstevel@tonic-gate 				return (0);
111*0Sstevel@tonic-gate 			}
112*0Sstevel@tonic-gate 			as->as_off = get4(off);
113*0Sstevel@tonic-gate 			as->as_name = str;
114*0Sstevel@tonic-gate 			as->as_hash = elf_hash(str);
115*0Sstevel@tonic-gate 			++as;
116*0Sstevel@tonic-gate 			off += 4;
117*0Sstevel@tonic-gate 			while (*str++ != '\0')
118*0Sstevel@tonic-gate 				/* LINTED */
119*0Sstevel@tonic-gate 				;
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 		as->as_name = 0;
122*0Sstevel@tonic-gate 		as->as_off = 0;
123*0Sstevel@tonic-gate 		as->as_hash = ~(unsigned long)0L;
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 	return (oas);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate Elf_Arsym *
130*0Sstevel@tonic-gate elf_getarsym(Elf * elf, size_t * ptr)
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	Byte *		as;
133*0Sstevel@tonic-gate 	size_t		sz;
134*0Sstevel@tonic-gate 	Elf_Arsym *	rc;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (ptr != 0)
137*0Sstevel@tonic-gate 		*ptr = 0;
138*0Sstevel@tonic-gate 	if (elf == 0)
139*0Sstevel@tonic-gate 		return (0);
140*0Sstevel@tonic-gate 	ELFRLOCK(elf);
141*0Sstevel@tonic-gate 	if (elf->ed_kind != ELF_K_AR) {
142*0Sstevel@tonic-gate 		ELFUNLOCK(elf);
143*0Sstevel@tonic-gate 		_elf_seterr(EREQ_AR, 0);
144*0Sstevel@tonic-gate 		return (0);
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 	if ((as = (Byte *)elf->ed_arsym) == 0) {
147*0Sstevel@tonic-gate 		ELFUNLOCK(elf);
148*0Sstevel@tonic-gate 		return (0);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 	if (elf->ed_myflags & EDF_ASALLOC) {
151*0Sstevel@tonic-gate 		if (ptr != 0)
152*0Sstevel@tonic-gate 			*ptr = elf->ed_arsymsz;
153*0Sstevel@tonic-gate 		ELFUNLOCK(elf);
154*0Sstevel@tonic-gate 		/* LINTED */
155*0Sstevel@tonic-gate 		return ((Elf_Arsym *)as);
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	/*
158*0Sstevel@tonic-gate 	 * We're gonna need a write lock.
159*0Sstevel@tonic-gate 	 */
160*0Sstevel@tonic-gate 	ELFUNLOCK(elf)
161*0Sstevel@tonic-gate 	ELFWLOCK(elf)
162*0Sstevel@tonic-gate 	sz = elf->ed_arsymsz;
163*0Sstevel@tonic-gate 	if (_elf_vm(elf, (size_t)(as - (Byte *)elf->ed_ident), sz) !=
164*0Sstevel@tonic-gate 	    OK_YES) {
165*0Sstevel@tonic-gate 		ELFUNLOCK(elf);
166*0Sstevel@tonic-gate 		return (0);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	if ((elf->ed_arsym = arsym(as, sz, &elf->ed_arsymsz)) == 0) {
169*0Sstevel@tonic-gate 		ELFUNLOCK(elf);
170*0Sstevel@tonic-gate 		return (0);
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	elf->ed_myflags |= EDF_ASALLOC;
173*0Sstevel@tonic-gate 	if (ptr != 0)
174*0Sstevel@tonic-gate 		*ptr = elf->ed_arsymsz;
175*0Sstevel@tonic-gate 	rc = (Elf_Arsym *)elf->ed_arsym;
176*0Sstevel@tonic-gate 	ELFUNLOCK(elf);
177*0Sstevel@tonic-gate 	return (rc);
178*0Sstevel@tonic-gate }
179