xref: /titanic_50/usr/src/cmd/sgs/libld/common/support.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include	<stdio.h>
30 #include	<dlfcn.h>
31 #include	<libelf.h>
32 #include	<link.h>
33 #include	<debug.h>
34 #include	"msg.h"
35 #include	"_libld.h"
36 
37 /*
38  * Table which defines the default functions to be called by the library
39  * SUPPORT (-S <libname>).  These functions can be redefined by the
40  * ld_support_loadso() routine.
41  */
42 static Support_list support[LDS_NUM] = {
43 	{MSG_ORIG(MSG_SUP_VERSION),	{ 0, 0 }},	/* LDS_VERSION */
44 	{MSG_ORIG(MSG_SUP_INPUT_DONE),	{ 0, 0 }},	/* LDS_INPUT_DONE */
45 #if	defined(_ELF64)
46 	{MSG_ORIG(MSG_SUP_START_64),	{ 0, 0 }},	/* LDS_START64 */
47 	{MSG_ORIG(MSG_SUP_ATEXIT_64),	{ 0, 0 }},	/* LDS_ATEXIT64 */
48 	{MSG_ORIG(MSG_SUP_FILE_64),	{ 0, 0 }},	/* LDS_FILE64 */
49 	{MSG_ORIG(MSG_SUP_INP_SECTION_64), { 0, 0 }},	/* LDS_INP_SECTION64 */
50 	{MSG_ORIG(MSG_SUP_SECTION_64),	{ 0, 0 }}	/* LDS_SECTION64 */
51 #else	/* Elf32 */
52 	{MSG_ORIG(MSG_SUP_START),	{ 0, 0 }},	/* LDS_START */
53 	{MSG_ORIG(MSG_SUP_ATEXIT),	{ 0, 0 }},	/* LDS_ATEXIT */
54 	{MSG_ORIG(MSG_SUP_FILE),	{ 0, 0 }},	/* LDS_FILE */
55 	{MSG_ORIG(MSG_SUP_INP_SECTION),	{ 0, 0 }},	/* LDS_INP_SECTION */
56 	{MSG_ORIG(MSG_SUP_SECTION),	{ 0, 0 }}	/* LDS_SECTION */
57 #endif
58 };
59 
60 /*
61  * Loads in a support shared object specified using the SGS_SUPPORT environment
62  * variable or the -S ld option, and determines which interface functions are
63  * provided by that object.
64  */
65 uintptr_t
66 ld_sup_loadso(Ofl_desc *ofl, const char *obj)
67 {
68 	void		*handle;
69 	void		(*fptr)();
70 	Func_list	*flp;
71 	int 		i;
72 	uint_t		ver_level;
73 
74 	/*
75 	 * Load the required support library.  If we are unable to load it fail
76 	 * with a fatal error.
77 	 */
78 	if ((handle = dlopen(obj, RTLD_LAZY)) == NULL) {
79 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SUP_NOLOAD),
80 		    obj, dlerror());
81 		return (S_ERROR);
82 	}
83 
84 	ver_level = LD_SUP_VERSION1;
85 	for (i = 0; i < LDS_NUM; i++) {
86 		if (fptr = (void (*)())dlsym(handle, support[i].sup_name)) {
87 
88 			if ((flp = libld_malloc(sizeof (Func_list))) == NULL)
89 				return (S_ERROR);
90 
91 			flp->fl_obj = obj;
92 			flp->fl_fptr = fptr;
93 			DBG_CALL(Dbg_support_load(ofl->ofl_lml, obj,
94 			    support[i].sup_name));
95 
96 			if (i == LDS_VERSION) {
97 				DBG_CALL(Dbg_support_action(ofl->ofl_lml,
98 				    flp->fl_obj, support[LDS_VERSION].sup_name,
99 				    LDS_VERSION, 0));
100 				ver_level = ((uint_t(*)())
101 				    flp->fl_fptr)(LD_SUP_VCURRENT);
102 				if ((ver_level == LD_SUP_VNONE) ||
103 				    (ver_level > LD_SUP_VCURRENT)) {
104 					eprintf(ofl->ofl_lml, ERR_FATAL,
105 					    MSG_INTL(MSG_SUP_BADVERSION),
106 					    LD_SUP_VCURRENT, ver_level);
107 					(void) dlclose(handle);
108 					return (S_ERROR);
109 				}
110 
111 			}
112 			flp->fl_version = ver_level;
113 			if (list_appendc(&support[i].sup_funcs, flp) == 0)
114 				return (S_ERROR);
115 		}
116 	}
117 	return (1);
118 }
119 
120 /*
121  * Wrapper routines for the ld support library calls.
122  */
123 void
124 ld_sup_start(Ofl_desc *ofl, const Half etype, const char *caller)
125 {
126 	Func_list	*flp;
127 	Listnode	*lnp;
128 
129 	for (LIST_TRAVERSE(&support[LDS_START].sup_funcs, lnp, flp)) {
130 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
131 		    support[LDS_START].sup_name, LDS_START, ofl->ofl_name));
132 		(*flp->fl_fptr)(ofl->ofl_name, etype, caller);
133 	}
134 }
135 
136 void
137 ld_sup_atexit(Ofl_desc *ofl, int ecode)
138 {
139 	Func_list	*flp;
140 	Listnode	*lnp;
141 
142 	for (LIST_TRAVERSE(&support[LDS_ATEXIT].sup_funcs, lnp, flp)) {
143 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
144 		    support[LDS_ATEXIT].sup_name, LDS_ATEXIT, 0));
145 		(*flp->fl_fptr)(ecode);
146 	}
147 }
148 
149 void
150 ld_sup_file(Ofl_desc *ofl, const char *ifile, const Elf_Kind ekind, int flags,
151     Elf *elf)
152 {
153 	Func_list	*flp;
154 	Listnode	*lnp;
155 
156 	for (LIST_TRAVERSE(&support[LDS_FILE].sup_funcs, lnp, flp)) {
157 		int	_flags = 0;
158 
159 		if (!(flags & FLG_IF_CMDLINE))
160 			_flags |= LD_SUP_DERIVED;
161 		if (!(flags & FLG_IF_NEEDED))
162 			_flags |= LD_SUP_INHERITED;
163 		if (flags & FLG_IF_EXTRACT)
164 			_flags |= LD_SUP_EXTRACTED;
165 
166 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
167 		    support[LDS_FILE].sup_name, LDS_FILE, ifile));
168 		(*flp->fl_fptr)(ifile, ekind, _flags, elf);
169 	}
170 }
171 
172 uintptr_t
173 ld_sup_input_section(Ofl_desc *ofl, Ifl_desc *ifl, const char *sname,
174     Shdr **oshdr, Word ndx, Elf_Scn *scn, Elf *elf)
175 {
176 	Func_list	*flp;
177 	Listnode	*lnp;
178 	uint_t		flags = 0;
179 	Elf_Data	*data = NULL;
180 	Shdr		*nshdr = *oshdr;
181 
182 	for (LIST_TRAVERSE(&support[LDS_INP_SECTION].sup_funcs, lnp, flp)) {
183 		/*
184 		 * This interface was introduced in VERSION2 - so only call it
185 		 * for libraries reporting support for version 2 or above.
186 		 */
187 		if (flp->fl_version < LD_SUP_VERSION2)
188 			continue;
189 		if ((data == NULL) &&
190 		    ((data = elf_getdata(scn, NULL)) == NULL)) {
191 			eprintf(ofl->ofl_lml, ERR_ELF,
192 			    MSG_INTL(MSG_ELF_GETDATA), ifl->ifl_name);
193 			ofl->ofl_flags |= FLG_OF_FATAL;
194 			return (S_ERROR);
195 		}
196 
197 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
198 		    support[LDS_INP_SECTION].sup_name, LDS_INP_SECTION, sname));
199 		(*flp->fl_fptr)(sname, &nshdr, ndx, data, elf, &flags);
200 	}
201 
202 	/*
203 	 * If the section header has been re-allocated (known to occur with
204 	 * libCCexcept.so), then diagnose the section header difference and
205 	 * return the new section header.
206 	 */
207 	if (nshdr != *oshdr) {
208 		Dbg_shdr_modified(ofl->ofl_lml, ifl->ifl_ehdr->e_machine,
209 		    *oshdr, nshdr, sname);
210 		*oshdr = nshdr;
211 	}
212 	return (0);
213 }
214 
215 void
216 ld_sup_section(Ofl_desc *ofl, const char *scn, Shdr *shdr, Word ndx,
217     Elf_Data *data, Elf *elf)
218 {
219 	Func_list	*flp;
220 	Listnode	*lnp;
221 
222 	for (LIST_TRAVERSE(&support[LDS_SECTION].sup_funcs, lnp, flp)) {
223 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
224 		    support[LDS_SECTION].sup_name, LDS_SECTION, scn));
225 		(*flp->fl_fptr)(scn, shdr, ndx, data, elf);
226 	}
227 }
228 
229 void
230 ld_sup_input_done(Ofl_desc *ofl)
231 {
232 	Func_list	*flp;
233 	Listnode	*lnp;
234 	uint_t		flags = 0;
235 
236 	for (LIST_TRAVERSE(&support[LDS_INPUT_DONE].sup_funcs, lnp, flp)) {
237 		/*
238 		 * This interface was introduced in VERSION2 - so only call it
239 		 * for libraries reporting support for version 2 or above.
240 		 */
241 		if (flp->fl_version < LD_SUP_VERSION2)
242 			continue;
243 		DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
244 		    support[LDS_INPUT_DONE].sup_name, LDS_INPUT_DONE, 0));
245 		(*flp->fl_fptr)(&flags);
246 	}
247 }
248