xref: /titanic_41/usr/src/cmd/sgs/libld/common/support.c (revision 26f3cdf03f1adcc98f6d3d99843ee71e9229a8c0)
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 (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include	<stdio.h>
27 #include	<dlfcn.h>
28 #include	<libelf.h>
29 #include	<link.h>
30 #include	<debug.h>
31 #include	"msg.h"
32 #include	"_libld.h"
33 
34 /*
35  * Table which defines the default functions to be called by the library
36  * SUPPORT (-S <libname>).  These functions can be redefined by the
37  * ld_support_loadso() routine.
38  */
39 static Support_list support[LDS_NUM] = {
40 	{MSG_ORIG(MSG_SUP_VERSION),	NULL},	/* LDS_VERSION */
41 	{MSG_ORIG(MSG_SUP_INPUT_DONE),	NULL},	/* LDS_INPUT_DONE */
42 #if	defined(_ELF64)
43 	{MSG_ORIG(MSG_SUP_START_64),	NULL},	/* LDS_START */
44 	{MSG_ORIG(MSG_SUP_ATEXIT_64),	NULL},	/* LDS_ATEXIT */
45 	{MSG_ORIG(MSG_SUP_OPEN_64),	NULL},	/* LDS_OPEN */
46 	{MSG_ORIG(MSG_SUP_FILE_64),	NULL},	/* LDS_FILE */
47 	{MSG_ORIG(MSG_SUP_INSEC_64),	NULL},	/* LDS_INSEC */
48 	{MSG_ORIG(MSG_SUP_SEC_64),	NULL}	/* LDS_SEC */
49 #else	/* Elf32 */
50 	{MSG_ORIG(MSG_SUP_START),	NULL},	/* LDS_START */
51 	{MSG_ORIG(MSG_SUP_ATEXIT),	NULL},	/* LDS_ATEXIT */
52 	{MSG_ORIG(MSG_SUP_OPEN),	NULL},	/* LDS_OPEN */
53 	{MSG_ORIG(MSG_SUP_FILE),	NULL},	/* LDS_FILE */
54 	{MSG_ORIG(MSG_SUP_INSEC),	NULL},	/* LDS_INSEC */
55 	{MSG_ORIG(MSG_SUP_SEC),		NULL}	/* LDS_SEC */
56 #endif
57 };
58 
59 /*
60  * Loads in a support shared object specified using the SGS_SUPPORT environment
61  * variable or the -S ld option, and determines which interface functions are
62  * provided by that object.
63  */
64 uintptr_t
65 ld_sup_loadso(Ofl_desc *ofl, const char *obj)
66 {
67 	void	*handle, (*fptr)();
68 	uint_t	interface, version = LD_SUP_VERSION1;
69 
70 	/*
71 	 * Load the required support library.  If we are unable to load it fail
72 	 * with a fatal error.
73 	 */
74 	if ((handle = dlopen(obj, (RTLD_LAZY | RTLD_FIRST))) == NULL) {
75 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SUP_NOLOAD),
76 		    obj, dlerror());
77 		return (S_ERROR);
78 	}
79 
80 	for (interface = 0; interface < LDS_NUM; interface++) {
81 		Func_list	fl;
82 
83 		if ((fptr = (void (*)())dlsym(handle,
84 		    support[interface].sup_name)) == NULL)
85 			continue;
86 
87 		DBG_CALL(Dbg_support_load(ofl->ofl_lml, obj,
88 		    support[interface].sup_name));
89 
90 		if (interface == LDS_VERSION) {
91 			DBG_CALL(Dbg_support_action(ofl->ofl_lml, obj,
92 			    support[LDS_VERSION].sup_name, LDS_VERSION, 0));
93 
94 			version = ((uint_t(*)())fptr)(LD_SUP_VCURRENT);
95 
96 			/*
97 			 * If version is LD_SUP_VNONE, unload the support
98 			 * library and act as if it was never requested.
99 			 * This provides the support library with a mechanism
100 			 * for opting out of the process.
101 			 *
102 			 * Note that this depends on LDS_VERSION being the
103 			 * first item in support[]. If this were not true,
104 			 * then other functions from the library might be
105 			 * entered into the function lists, and unloading
106 			 * the object would cause corruption.
107 			 */
108 			assert(LDS_VERSION == 0);
109 			if (version == LD_SUP_VNONE) {
110 				DBG_CALL(Dbg_support_vnone(ofl->ofl_lml,
111 				    obj));
112 				(void) dlclose(handle);
113 				return (1);
114 			}
115 
116 			/*
117 			 * If the support library has a version higher
118 			 * than we support, we are unable to accept it.
119 			 */
120 			if (version > LD_SUP_VCURRENT) {
121 				ld_eprintf(ofl, ERR_FATAL,
122 				    MSG_INTL(MSG_SUP_BADVERSION), obj,
123 				    LD_SUP_VCURRENT, version);
124 				(void) dlclose(handle);
125 				return (S_ERROR);
126 			}
127 		}
128 
129 		fl.fl_obj = obj;
130 		fl.fl_fptr = fptr;
131 		fl.fl_version = version;
132 		if (alist_append(&support[interface].sup_funcs, &fl,
133 		    sizeof (Func_list), AL_CNT_SUPPORT) == NULL)
134 			return (S_ERROR);
135 	}
136 	return (1);
137 }
138 
139 /*
140  * Wrapper routines for the ld support library calls.
141  */
142 void
143 ld_sup_start(Ofl_desc *ofl, const Half etype, const char *caller)
144 {
145 	Func_list	*flp;
146 	Aliste		idx;
147 
148 	for (ALIST_TRAVERSE(support[LDS_START].sup_funcs, idx, flp)) {
149 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
150 		    support[LDS_START].sup_name, LDS_START, ofl->ofl_name));
151 		(*flp->fl_fptr)(ofl->ofl_name, etype, caller);
152 	}
153 }
154 
155 void
156 ld_sup_atexit(Ofl_desc *ofl, int ecode)
157 {
158 	Func_list	*flp;
159 	Aliste		idx;
160 
161 	for (ALIST_TRAVERSE(support[LDS_ATEXIT].sup_funcs, idx, flp)) {
162 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
163 		    support[LDS_ATEXIT].sup_name, LDS_ATEXIT, 0));
164 		(*flp->fl_fptr)(ecode);
165 	}
166 }
167 
168 void
169 ld_sup_open(Ofl_desc *ofl, const char **opath, const char **ofile, int *ofd,
170     int flags, Elf **oelf, Elf *ref, size_t off, const Elf_Kind ekind)
171 {
172 	Func_list	*flp;
173 	Aliste		idx;
174 
175 	for (ALIST_TRAVERSE(support[LDS_OPEN].sup_funcs, idx, flp)) {
176 		const char	*npath = *opath;
177 		const char	*nfile = *ofile;
178 		Elf		*nelf = *oelf;
179 		int		nfd = *ofd;
180 		int		_flags = 0;
181 
182 		/*
183 		 * This interface was introduced in VERSION3.  Only call this
184 		 * function for libraries reporting support for version 3 or
185 		 * above.
186 		 */
187 		if (flp->fl_version < LD_SUP_VERSION3)
188 			continue;
189 
190 		if (!(flags & FLG_IF_CMDLINE))
191 			_flags |= LD_SUP_DERIVED;
192 		if (!(flags & FLG_IF_NEEDED))
193 			_flags |= LD_SUP_INHERITED;
194 		if (flags & FLG_IF_EXTRACT)
195 			_flags |= LD_SUP_EXTRACTED;
196 
197 		/*
198 		 * If the present object is an extracted archive member, make
199 		 * sure the archive offset is reset so that the caller can
200 		 * obtain an ELF descriptor to the same member (an elf_begin()
201 		 * moves the offset to the next member).
202 		 */
203 		if (flags & FLG_IF_EXTRACT)
204 			(void) elf_rand(ref, off);
205 
206 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
207 		    support[LDS_OPEN].sup_name, LDS_OPEN, *opath));
208 		(*flp->fl_fptr)(&npath, &nfile, &nfd, _flags, &nelf, ref, off,
209 		    ekind);
210 
211 		/*
212 		 * If the file descriptor, ELF descriptor, or file names have
213 		 * been modified, then diagnose the differences and return the
214 		 * new data.  As a basic test, make sure the support library
215 		 * hasn't nulled out data ld(1) will try and dereference.
216 		 */
217 		if ((npath != *opath) || (nfd != *ofd) || (nelf != *oelf)) {
218 			DBG_CALL(Dbg_file_modified(ofl->ofl_lml, flp->fl_obj,
219 			    *opath, npath, *ofd, nfd, *oelf, nelf));
220 			if (npath)
221 				*opath = npath;
222 			if (nfile)
223 				*ofile = nfile;
224 			*ofd = nfd;
225 			*oelf = nelf;
226 		}
227 	}
228 }
229 
230 void
231 ld_sup_file(Ofl_desc *ofl, const char *ifile, const Elf_Kind ekind, int flags,
232     Elf *elf)
233 {
234 	Func_list	*flp;
235 	Aliste		idx;
236 
237 	for (ALIST_TRAVERSE(support[LDS_FILE].sup_funcs, idx, flp)) {
238 		int	_flags = 0;
239 
240 		if (!(flags & FLG_IF_CMDLINE))
241 			_flags |= LD_SUP_DERIVED;
242 		if (!(flags & FLG_IF_NEEDED))
243 			_flags |= LD_SUP_INHERITED;
244 		if (flags & FLG_IF_EXTRACT)
245 			_flags |= LD_SUP_EXTRACTED;
246 
247 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
248 		    support[LDS_FILE].sup_name, LDS_FILE, ifile));
249 		(*flp->fl_fptr)(ifile, ekind, _flags, elf);
250 	}
251 }
252 
253 uintptr_t
254 ld_sup_input_section(Ofl_desc *ofl, Ifl_desc *ifl, const char *sname,
255     Shdr **oshdr, Word ndx, Elf_Scn *scn, Elf *elf)
256 {
257 	Func_list	*flp;
258 	Aliste		idx;
259 	uint_t		flags = 0;
260 	Elf_Data	*data = NULL;
261 
262 	for (ALIST_TRAVERSE(support[LDS_INSEC].sup_funcs, idx, flp)) {
263 		Shdr	*nshdr = *oshdr;
264 
265 		/*
266 		 * This interface was introduced in VERSION2.  Only call this
267 		 * function for libraries reporting support for version 2 or
268 		 * above.
269 		 */
270 		if (flp->fl_version < LD_SUP_VERSION2)
271 			continue;
272 
273 		if ((data == NULL) &&
274 		    ((data = elf_getdata(scn, NULL)) == NULL)) {
275 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
276 			    ifl->ifl_name);
277 			return (S_ERROR);
278 		}
279 
280 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
281 		    support[LDS_INSEC].sup_name, LDS_INSEC, sname));
282 		(*flp->fl_fptr)(sname, &nshdr, ndx, data, elf, &flags);
283 
284 		/*
285 		 * If the section header has been re-allocated (known to occur
286 		 * with libCCexcept.so), then diagnose the section header
287 		 * difference and return the new section header.
288 		 */
289 		if (nshdr != *oshdr) {
290 			Ehdr	*ehdr = ifl->ifl_ehdr;
291 
292 			DBG_CALL(Dbg_shdr_modified(ofl->ofl_lml, flp->fl_obj,
293 			    ehdr->e_ident[EI_OSABI], ehdr->e_machine, ndx,
294 			    *oshdr, nshdr, sname));
295 			*oshdr = nshdr;
296 		}
297 	}
298 	return (0);
299 }
300 
301 void
302 ld_sup_section(Ofl_desc *ofl, const char *scn, Shdr *shdr, Word ndx,
303     Elf_Data *data, Elf *elf)
304 {
305 	Func_list	*flp;
306 	Aliste		idx;
307 
308 	for (ALIST_TRAVERSE(support[LDS_SEC].sup_funcs, idx, flp)) {
309 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
310 		    support[LDS_SEC].sup_name, LDS_SEC, scn));
311 		(*flp->fl_fptr)(scn, shdr, ndx, data, elf);
312 	}
313 }
314 
315 void
316 ld_sup_input_done(Ofl_desc *ofl)
317 {
318 	Func_list	*flp;
319 	Aliste		idx;
320 	uint_t		flags = 0;
321 
322 	for (ALIST_TRAVERSE(support[LDS_INPUT_DONE].sup_funcs, idx, flp)) {
323 		/*
324 		 * This interface was introduced in VERSION2.  Only call this
325 		 * function for libraries reporting support for version 2 or
326 		 * above.
327 		 */
328 		if (flp->fl_version < LD_SUP_VERSION2)
329 			continue;
330 
331 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
332 		    support[LDS_INPUT_DONE].sup_name, LDS_INPUT_DONE, 0));
333 		(*flp->fl_fptr)(&flags);
334 	}
335 }
336