xref: /titanic_41/usr/src/cmd/sgs/rtld/common/object.c (revision eb1a34638eba7c5add1421327f3eb225a8ea7518)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Object file dependent suport for ELF objects.
29  */
30 
31 #include	<sys/mman.h>
32 #include	<stdio.h>
33 #include	<unistd.h>
34 #include	<libelf.h>
35 #include	<string.h>
36 #include	<dlfcn.h>
37 #include	<debug.h>
38 #include	<libld.h>
39 #include	"_rtld.h"
40 #include	"_audit.h"
41 #include	"_elf.h"
42 
43 static Rt_map	*olmp = NULL;
44 static Alist	*mpalp = NULL;
45 
46 static Ehdr	dehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
47 			    M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
48 
49 /*
50  * Process a relocatable object.  The static object link map pointer is used as
51  * a flag to determine whether a concatenation is already in progress (ie. an
52  * LD_PRELOAD may specify a list of objects).  The link map returned simply
53  * specifies an `object' flag which the caller can interpret and thus call
54  * elf_obj_fini() to complete the concatenation.
55  */
56 static Rt_map *
57 elf_obj_init(Lm_list *lml, Aliste lmco, const char *oname)
58 {
59 	Ofl_desc	*ofl;
60 	const char	*name;
61 	size_t		lmsz;
62 
63 	/*
64 	 * Allocate the name of this object, as the original name may be
65 	 * associated with a data buffer that can be reused to load the
66 	 * dependencies needed to processes this object.
67 	 */
68 	if ((name = stravl_insert(oname, 0, 0, 0)) == NULL)
69 		return (NULL);
70 
71 	/*
72 	 * Initialize an output file descriptor and the entrance criteria.
73 	 */
74 	if ((ofl = calloc(sizeof (Ofl_desc), 1)) == NULL)
75 		return (NULL);
76 
77 	ofl->ofl_dehdr = &dehdr;
78 
79 	ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP);
80 	ofl->ofl_flags1 = (FLG_OF1_RELDYN | FLG_OF1_TEXTOFF | FLG_OF1_MEMORY);
81 	ofl->ofl_lml = lml;
82 
83 	/*
84 	 * As ent_setup() will effectively lazy load the necessary support
85 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
86 	 * Then configure libld.so to process objects of the desired target
87 	 * type (this is the first call to libld.so, which will effectively
88 	 * lazyload it).
89 	 */
90 	if ((elf_rtld_load() == 0) || (ld_init_target(lml, M_MACH) != 0)) {
91 		free(ofl);
92 		return (NULL);
93 	}
94 
95 	/*
96 	 * Obtain a generic set of entrance criteria, and generate a link map
97 	 * place holder and use the ELFPRV() element to maintain the output
98 	 * file descriptor.
99 	 */
100 	lmsz = S_DROUND(sizeof (Rt_map)) + sizeof (Rt_elfp);
101 	if ((ld_ent_setup(ofl, syspagsz) == S_ERROR) ||
102 	    ((olmp = calloc(lmsz, 1)) == NULL)) {
103 		free(ofl);
104 		return (NULL);
105 	}
106 
107 	DBG_CALL(Dbg_file_elf(lml, name, 0, 0, lml->lm_lmidstr, lmco));
108 	FLAGS(olmp) |= FLG_RT_OBJECT;
109 	ELFPRV(olmp) = (void *)ofl;
110 
111 	/*
112 	 * Initialize string tables.
113 	 */
114 	if (ld_init_strings(ofl) == S_ERROR) {
115 		free(ofl);
116 		free(olmp);
117 		olmp = NULL;
118 		return (NULL);
119 	}
120 
121 	/*
122 	 * Assign the output file name to be the initial object that got us
123 	 * here.  This name is being used for diagnostic purposes only as we
124 	 * don't actually generate an output file unless debugging is enabled.
125 	 */
126 	ofl->ofl_name = name;
127 	NAME(olmp) = (char *)name;
128 	LIST(olmp) = lml;
129 
130 	lm_append(lml, lmco, olmp);
131 	return (olmp);
132 }
133 
134 /*
135  * Define a structure to retain the mapping information of the original
136  * relocatable object.  Typically, mmapobj(2) maps a relocatable object into one
137  * mapping.  However, if padding has been enabled by a debugger, then additional
138  * padding segments may have been added.  elf_obj_file() needs to know which
139  * segment is the relocatable objects data, and retain the initial segment and
140  * the associated segment number for unmapping this object later (see
141  * elf_obj_fini()).  Note, even if padding is enabled, the final shared object
142  * that is created by the link-editor for this relocatable object will have no
143  * associated padding, as ld(1) has no capabilities to provide padding.
144  */
145 typedef struct {
146 	mmapobj_result_t	*md_mpp;
147 	uint_t			md_mnum;
148 } Mmap_desc;
149 
150 /*
151  * Initial processing of a relocatable object.  If this is the first object
152  * encountered we need to initialize some structures, then simply call the
153  * link-edit functionality to provide the initial processing of the file (ie.
154  * reads in sections and symbols, performs symbol resolution if more that one
155  * object file have been specified, and assigns input sections to output
156  * sections).
157  */
158 Rt_map *
159 elf_obj_file(Lm_list *lml, Aliste lmco, const char *name,
160     mmapobj_result_t *hmpp, mmapobj_result_t *mpp, uint_t mnum)
161 {
162 	Rej_desc	rej;
163 	Mmap_desc	md;
164 
165 	/*
166 	 * If this is the first relocatable object (LD_PRELOAD could provide a
167 	 * list of objects), initialize an input file descriptor and a link map.
168 	 */
169 	if ((olmp == NULL) && ((olmp = elf_obj_init(lml, lmco, name)) == NULL))
170 		return (NULL);
171 
172 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
173 
174 	/*
175 	 * Keep track of the input image, as this must be free'd after all ELF
176 	 * processing is completed.
177 	 */
178 	md.md_mpp = mpp;
179 	md.md_mnum = mnum;
180 	if (alist_append(&mpalp, &md, sizeof (Mmap_desc),
181 	    AL_CNT_MPOBJS) == NULL) {
182 		remove_so(lml, olmp);
183 		return (NULL);
184 	}
185 
186 	/*
187 	 * Pass the object mapping to the link-editor to commence processing the
188 	 * file.
189 	 */
190 	if (ld_process_mem(name, name, hmpp->mr_addr, hmpp->mr_msize,
191 	    (Ofl_desc *)ELFPRV(olmp), &rej) == (Ifl_desc *)S_ERROR) {
192 		remove_so(lml, olmp);
193 		return (NULL);
194 	}
195 
196 	return (olmp);
197 }
198 
199 /*
200  * Finish relocatable object processing.  Having already initially processed one
201  * or more objects, complete the generation of a shared object image by calling
202  * the appropriate link-edit functionality (refer to sgs/ld/common/main.c).
203  */
204 Rt_map *
205 elf_obj_fini(Lm_list *lml, Rt_map *lmp, int *in_nfavl)
206 {
207 	Ofl_desc		*ofl = (Ofl_desc *)ELFPRV(lmp);
208 	Rt_map			*nlmp, *tlmp;
209 	Ehdr			*ehdr;
210 	Phdr			*phdr;
211 	mmapobj_result_t	*mpp, *hmpp;
212 	uint_t			phnum;
213 	int			mnum;
214 	Lm_cntl			*lmc;
215 	Aliste			idx1;
216 	Mmap_desc		*mdp;
217 	Fdesc			fd = { 0 };
218 	Grp_hdl			*ghp;
219 	Rej_desc		rej = { 0 };
220 
221 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
222 
223 	if (ld_reloc_init(ofl) == S_ERROR)
224 		return (NULL);
225 	if (ld_sym_validate(ofl) == S_ERROR)
226 		return (NULL);
227 
228 	/*
229 	 * At this point, all input section processing is complete.  If any
230 	 * hardware or software capabilities have been established, ensure that
231 	 * they are appropriate for this platform.
232 	 */
233 	if ((ofl->ofl_hwcap_1) && (hwcap_check(ofl->ofl_hwcap_1, &rej) == 0)) {
234 		if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
235 		    (lmp != NULL) && (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
236 		    (NEXT(lmp) == NULL)) {
237 			(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
238 			    ofl->ofl_name, rej.rej_str);
239 		}
240 		return (NULL);
241 	}
242 
243 	if ((ofl->ofl_sfcap_1) && (sfcap_check(ofl->ofl_sfcap_1, &rej) == 0)) {
244 		if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
245 		    (lmp != NULL) && (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
246 		    (NEXT(lmp) == NULL)) {
247 			(void) printf(MSG_INTL(MSG_LDD_GEN_SFCAP_1),
248 			    ofl->ofl_name, rej.rej_str);
249 		}
250 		return (NULL);
251 	}
252 
253 	/*
254 	 * Finish creating the output file.
255 	 */
256 	if (ld_make_sections(ofl) == S_ERROR)
257 		return (NULL);
258 	if (ld_create_outfile(ofl) == S_ERROR)
259 		return (NULL);
260 	if (ld_update_outfile(ofl) == S_ERROR)
261 		return (NULL);
262 	if (ld_reloc_process(ofl) == S_ERROR)
263 		return (NULL);
264 
265 	/*
266 	 * At this point we have a memory image of the shared object.  The link
267 	 * editor would normally simply write this to the required output file.
268 	 * If we're debugging generate a standard temporary output file.
269 	 */
270 	DBG_CALL(Dbg_file_output(ofl));
271 
272 	/*
273 	 * Allocate a mapping array to retain mapped segment information.
274 	 */
275 	ehdr = ofl->ofl_nehdr;
276 	phdr = ofl->ofl_phdr;
277 
278 	if ((mpp = hmpp = calloc(ehdr->e_phnum,
279 	    sizeof (mmapobj_result_t))) == NULL)
280 		return (NULL);
281 	for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
282 		if (phdr[phnum].p_type != PT_LOAD)
283 			continue;
284 
285 		mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr +
286 		    (uintptr_t)ehdr);
287 		mpp[mnum].mr_msize = phdr[phnum].p_memsz;
288 		mpp[mnum].mr_fsize = phdr[phnum].p_filesz;
289 		mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
290 		mnum++;
291 	}
292 
293 	/*
294 	 * Generate a new link map representing the memory image created.
295 	 */
296 	fd.fd_nname = ofl->ofl_name;
297 	if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr,
298 	    ofl->ofl_size, 0, in_nfavl)) == NULL)
299 		return (NULL);
300 
301 	MMAPS(nlmp) = hmpp;
302 	MMAPCNT(nlmp) = mnum;
303 	PADSTART(nlmp) = (ulong_t)hmpp->mr_addr;
304 	PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr;
305 
306 	/*
307 	 * Replace the original (temporary) link map with the new link map.
308 	 */
309 	/* LINTED */
310 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
311 	lml->lm_obj--;
312 
313 	if ((tlmp = PREV_RT_MAP(nlmp)) == olmp)
314 		tlmp = nlmp;
315 
316 	if (PREV(olmp)) {
317 		NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp;
318 		PREV(nlmp) = PREV(olmp);
319 	} else {
320 		PREV(nlmp) = NULL;
321 		lmc->lc_head = nlmp;
322 		if (CNTL(nlmp) == ALIST_OFF_DATA)
323 			lml->lm_head = nlmp;
324 	}
325 
326 	if (NEXT(olmp) != (Link_map *)nlmp) {
327 		NEXT(nlmp) = NEXT(olmp);
328 		PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp;
329 	}
330 
331 	NEXT(tlmp) = NULL;
332 
333 	lmc->lc_tail = tlmp;
334 	if (CNTL(nlmp) == ALIST_OFF_DATA)
335 		lml->lm_tail = tlmp;
336 
337 	HANDLES(nlmp) = HANDLES(olmp);
338 	GROUPS(nlmp) = GROUPS(olmp);
339 	STDEV(nlmp) = STDEV(olmp);
340 	STINO(nlmp) = STINO(olmp);
341 
342 	FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
343 	FLAGS1(nlmp) |= FLAGS1(olmp);
344 	MODE(nlmp) |= MODE(olmp);
345 
346 	NAME(nlmp) = NAME(olmp);
347 
348 	/*
349 	 * Reassign any original handles to the new link-map.
350 	 */
351 	for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) {
352 		Grp_desc	*gdp;
353 		Aliste		idx2;
354 
355 		ghp->gh_ownlmp = nlmp;
356 
357 		for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
358 			if (gdp->gd_depend == olmp) {
359 				gdp->gd_depend = nlmp;
360 				break;
361 			}
362 		}
363 	}
364 
365 	ld_ofl_cleanup(ofl);
366 	free(ELFPRV(olmp));
367 	free(olmp);
368 	olmp = 0;
369 
370 	/*
371 	 * Unmap the original relocatable object.
372 	 */
373 	for (ALIST_TRAVERSE(mpalp, idx1, mdp)) {
374 		unmap_obj(mdp->md_mpp, mdp->md_mnum);
375 		free(mdp->md_mpp);
376 	}
377 	free(mpalp);
378 	mpalp = NULL;
379 
380 	/*
381 	 * Now that we've allocated our permanent link map structure, expand the
382 	 * PATHNAME() and insert this path name into the FullPathNode AVL tree.
383 	 */
384 	(void) fullpath(nlmp, 0);
385 	if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
386 		return (NULL);
387 
388 	/*
389 	 * If we're being audited tell the audit library of the file we've just
390 	 * opened.
391 	 */
392 	if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) {
393 		if (audit_objopen(lmp, lmp) == 0)
394 			return (NULL);
395 	}
396 	return (nlmp);
397 }
398