xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/object.c (revision d8d810637042c2a73a269f125dee75e8ed1421d1)
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) && lmp &&
235 		    (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
236 			(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
237 			    ofl->ofl_name, rej.rej_str);
238 		}
239 		return (NULL);
240 	}
241 
242 	if ((ofl->ofl_sfcap_1) && (sfcap_check(ofl->ofl_sfcap_1, &rej) == 0)) {
243 		if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp &&
244 		    (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
245 			(void) printf(MSG_INTL(MSG_LDD_GEN_SFCAP_1),
246 			    ofl->ofl_name, rej.rej_str);
247 		}
248 		return (NULL);
249 	}
250 
251 	/*
252 	 * Finish creating the output file.
253 	 */
254 	if (ld_make_sections(ofl) == S_ERROR)
255 		return (NULL);
256 	if (ld_create_outfile(ofl) == S_ERROR)
257 		return (NULL);
258 	if (ld_update_outfile(ofl) == S_ERROR)
259 		return (NULL);
260 	if (ld_reloc_process(ofl) == S_ERROR)
261 		return (NULL);
262 
263 	/*
264 	 * At this point we have a memory image of the shared object.  The link
265 	 * editor would normally simply write this to the required output file.
266 	 * If we're debugging generate a standard temporary output file.
267 	 */
268 	DBG_CALL(Dbg_file_output(ofl));
269 
270 	/*
271 	 * Allocate a mapping array to retain mapped segment information.
272 	 */
273 	ehdr = ofl->ofl_nehdr;
274 	phdr = ofl->ofl_phdr;
275 
276 	if ((mpp = hmpp = calloc(ehdr->e_phnum,
277 	    sizeof (mmapobj_result_t))) == NULL)
278 		return (NULL);
279 	for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
280 		if (phdr[phnum].p_type != PT_LOAD)
281 			continue;
282 
283 		mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr +
284 		    (uintptr_t)ehdr);
285 		mpp[mnum].mr_msize = phdr[phnum].p_memsz;
286 		mpp[mnum].mr_fsize = phdr[phnum].p_filesz;
287 		mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
288 		mnum++;
289 	}
290 
291 	/*
292 	 * Generate a new link map representing the memory image created.
293 	 */
294 	fd.fd_nname = ofl->ofl_name;
295 	if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr,
296 	    ofl->ofl_size, 0, in_nfavl)) == NULL)
297 		return (NULL);
298 
299 	MMAPS(nlmp) = hmpp;
300 	MMAPCNT(nlmp) = mnum;
301 	PADSTART(nlmp) = (ulong_t)hmpp->mr_addr;
302 	PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr;
303 
304 	/*
305 	 * Replace the original (temporary) link map with the new link map.
306 	 */
307 	/* LINTED */
308 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
309 	lml->lm_obj--;
310 
311 	if ((tlmp = PREV_RT_MAP(nlmp)) == olmp)
312 		tlmp = nlmp;
313 
314 	if (PREV(olmp)) {
315 		NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp;
316 		PREV(nlmp) = PREV(olmp);
317 	} else {
318 		PREV(nlmp) = NULL;
319 		lmc->lc_head = nlmp;
320 		if (CNTL(nlmp) == ALIST_OFF_DATA)
321 			lml->lm_head = nlmp;
322 	}
323 
324 	if (NEXT(olmp) != (Link_map *)nlmp) {
325 		NEXT(nlmp) = NEXT(olmp);
326 		PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp;
327 	}
328 
329 	NEXT(tlmp) = NULL;
330 
331 	lmc->lc_tail = tlmp;
332 	if (CNTL(nlmp) == ALIST_OFF_DATA)
333 		lml->lm_tail = tlmp;
334 
335 	HANDLES(nlmp) = HANDLES(olmp);
336 	GROUPS(nlmp) = GROUPS(olmp);
337 	STDEV(nlmp) = STDEV(olmp);
338 	STINO(nlmp) = STINO(olmp);
339 
340 	FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
341 	FLAGS1(nlmp) |= FLAGS1(olmp);
342 	MODE(nlmp) |= MODE(olmp);
343 
344 	NAME(nlmp) = NAME(olmp);
345 
346 	/*
347 	 * Reassign any original handles to the new link-map.
348 	 */
349 	for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) {
350 		Grp_desc	*gdp;
351 		Aliste		idx2;
352 
353 		ghp->gh_ownlmp = nlmp;
354 
355 		for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
356 			if (gdp->gd_depend == olmp) {
357 				gdp->gd_depend = nlmp;
358 				break;
359 			}
360 		}
361 	}
362 
363 	ld_ofl_cleanup(ofl);
364 	free(ELFPRV(olmp));
365 	free(olmp);
366 	olmp = 0;
367 
368 	/*
369 	 * Unmap the original relocatable object.
370 	 */
371 	for (ALIST_TRAVERSE(mpalp, idx1, mdp)) {
372 		unmap_obj(mdp->md_mpp, mdp->md_mnum);
373 		free(mdp->md_mpp);
374 	}
375 	free(mpalp);
376 	mpalp = NULL;
377 
378 	/*
379 	 * Now that we've allocated our permanent link map structure, expand the
380 	 * PATHNAME() and insert this path name into the FullPathNode AVL tree.
381 	 */
382 	(void) fullpath(nlmp, 0);
383 	if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
384 		return (NULL);
385 
386 	/*
387 	 * If we're being audited tell the audit library of the file we've just
388 	 * opened.
389 	 */
390 	if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) {
391 		if (audit_objopen(lmp, lmp) == 0)
392 			return (NULL);
393 	}
394 	return (nlmp);
395 }
396