xref: /titanic_51/usr/src/cmd/sgs/rtld/common/object.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Object file dependent suport for ELF objects.
30  */
31 #include	"_synonyms.h"
32 
33 #include	<sys/mman.h>
34 #include	<stdio.h>
35 #include	<unistd.h>
36 #include	<libelf.h>
37 #include	<string.h>
38 #include	<dlfcn.h>
39 #include	"libld.h"
40 #include	"_rtld.h"
41 #include	"_audit.h"
42 #include	"_elf.h"
43 #include	"debug.h"
44 
45 static Rt_map *		olmp = 0;
46 
47 /*
48  * Process a relocatable object.  The static object link map pointer is used as
49  * a flag to determine whether a concatenation is already in progress (ie. an
50  * LD_PRELOAD may specify a list of objects).  The link map returned simply
51  * specifies an `object' flag which the caller can interpret and thus call
52  * elf_obj_fini() to complete the concatenation.
53  */
54 static Rt_map *
55 elf_obj_init(Lm_list *lml, Aliste lmco, const char *name)
56 {
57 	Ofl_desc *	ofl;
58 
59 	/*
60 	 * Initialize an output file descriptor and the entrance criteria.
61 	 */
62 	if ((ofl = (Ofl_desc *)calloc(sizeof (Ofl_desc), 1)) == 0)
63 		return (0);
64 	ofl->ofl_e_machine = M_MACH;
65 	ofl->ofl_e_flags = 0;
66 	ofl->ofl_libver = EV_CURRENT;
67 	ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP |
68 		FLG_OF_MEMORY);
69 	ofl->ofl_flags1 = FLG_OF1_RELDYN | FLG_OF1_TEXTOFF;
70 
71 	/*
72 	 * As ent_setup() will effectively lazy load the necessary support
73 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
74 	 */
75 	if (elf_rtld_load() == 0)
76 		return (0);
77 
78 	/*
79 	 * Obtain a generic set of entrance criteria (this is the first call to
80 	 * libld.so, which will effectively lazyload it).
81 	 */
82 	if (ent_setup(ofl, syspagsz) == S_ERROR)
83 		return (0);
84 
85 	/*
86 	 * Generate a link map place holder and use the `rt_priv' element to
87 	 * maintain the output file descriptor.
88 	 */
89 	if ((olmp = (Rt_map *)calloc(sizeof (Rt_map), 1)) == 0)
90 		return (0);
91 
92 	DBG_CALL(Dbg_file_elf(name, 0, 0, 0, 0, get_linkmap_id(lml), lmco));
93 	FLAGS(olmp) |= FLG_RT_OBJECT;
94 	olmp->rt_priv = (void *)ofl;
95 
96 	/*
97 	 * Initialize string tables
98 	 */
99 	if (((ofl->ofl_shdrsttab = st_new(0)) == 0) ||
100 	    ((ofl->ofl_strtab = st_new(0)) == 0) ||
101 	    ((ofl->ofl_dynstrtab = st_new(0)) == 0))
102 		return (0);
103 
104 	/*
105 	 * Assign the output file name to be the initial object that got us
106 	 * here.  This name is being used for diagnostic purposes only as we
107 	 * don't actually generate an output file unless debugging is enabled.
108 	 */
109 	ofl->ofl_name = name;
110 	ORIGNAME(olmp) = PATHNAME(olmp) = NAME(olmp) = (char *)name;
111 	LIST(olmp) = lml;
112 
113 	lm_append(lml, lmco, olmp);
114 	return (olmp);
115 }
116 
117 /*
118  * Initial processing of a relocatable object.  If this is the first object
119  * encountered we need to initialize some structures, then simply call the
120  * link-edit functionality to provide the initial processing of the file (ie.
121  * reads in sections and symbols, performs symbol resolution if more that one
122  * object file have been specified, and assigns input sections to output
123  * sections).
124  */
125 Rt_map *
126 elf_obj_file(Lm_list *lml, Aliste lmco, const char *name, int fd)
127 {
128 	Rej_desc	rej;
129 
130 	/*
131 	 * If this is the first relocatable object (LD_PRELOAD could provide a
132 	 * list of objects), initialize an input file descriptor and a link map.
133 	 */
134 	if (!olmp) {
135 		/*
136 		 * Load the link-editor library.
137 		 */
138 		if ((olmp = elf_obj_init(lml, lmco, name)) == 0)
139 			return (0);
140 	}
141 
142 	/*
143 	 * Proceed to process the input file.
144 	 */
145 	DBG_CALL(Dbg_util_nl());
146 	if (process_open(name, 0, fd, (Ofl_desc *)olmp->rt_priv,
147 	    NULL, &rej) == (Ifl_desc *)S_ERROR)
148 		return (0);
149 
150 	return (olmp);
151 }
152 
153 /*
154  * Finish relocatable object processing.  Having already initially processed one
155  * or more objects, complete the generation of a shared object image by calling
156  * the appropriate link-edit functionality (refer to sgs/ld/common/main.c).
157  */
158 Rt_map *
159 elf_obj_fini(Lm_list *lml, Rt_map *lmp)
160 {
161 	Ofl_desc	*ofl = (Ofl_desc *)lmp->rt_priv;
162 	Rt_map		*nlmp;
163 	Addr		etext;
164 	Ehdr		*ehdr;
165 	Phdr		*phdr;
166 	Mmap		*mmaps;
167 	uint_t		phnum, mmapcnt;
168 	Lm_cntl 	*lmc;
169 
170 	DBG_CALL(Dbg_util_nl());
171 
172 	if (reloc_init(ofl) == S_ERROR)
173 		return (0);
174 	if (sym_validate(ofl) == S_ERROR)
175 		return (0);
176 	if (make_sections(ofl) == S_ERROR)
177 		return (0);
178 	if (create_outfile(ofl) == S_ERROR)
179 		return (0);
180 	if ((etext = update_outfile(ofl)) == (Addr)S_ERROR)
181 		return (0);
182 	if (reloc_process(ofl) == S_ERROR)
183 		return (0);
184 
185 	/*
186 	 * At this point we have a memory image of the shared object.  The link
187 	 * editor would normally simply write this to the required output file.
188 	 * If we're debugging generate a standard temporary output file.
189 	 */
190 	DBG_CALL(Dbg_file_output(ofl));
191 
192 	/*
193 	 * Allocate a mapping array to retain mapped segment information.
194 	 */
195 	ehdr = ofl->ofl_ehdr;
196 	phdr = ofl->ofl_phdr;
197 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
198 		return (0);
199 	for (mmapcnt = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
200 		if (phdr[phnum].p_type != PT_LOAD)
201 			continue;
202 
203 		mmaps[mmapcnt].m_vaddr = (caddr_t)
204 		    (phdr[phnum].p_vaddr + (ulong_t)ehdr);
205 		mmaps[mmapcnt].m_msize = phdr[phnum].p_memsz;
206 		mmaps[mmapcnt].m_fsize = phdr[phnum].p_filesz;
207 		mmaps[mmapcnt].m_perm = (PROT_READ | PROT_WRITE | PROT_EXEC);
208 		mmapcnt++;
209 	}
210 
211 	/*
212 	 * Generate a new link map representing the memory image created.
213 	 */
214 	if ((nlmp = elf_new_lm(lml, ofl->ofl_name, ofl->ofl_name,
215 	    ofl->ofl_osdynamic->os_outdata->d_buf, (ulong_t)ehdr,
216 	    (ulong_t)ehdr + etext, CNTL(olmp), (ulong_t)ofl->ofl_size,
217 	    0, 0, 0, mmaps, mmapcnt)) == 0)
218 		return (0);
219 
220 	/*
221 	 * Remove this link map from the end of the link map list and copy its
222 	 * contents into the link map originally created for this file (we copy
223 	 * the contents rather than manipulate the link map pointers as parts
224 	 * of the dlopen code have remembered the original link map address).
225 	 */
226 	NEXT((Rt_map *)PREV(nlmp)) = 0;
227 	/* LINTED */
228 	lmc = (Lm_cntl *)((char *)lml->lm_lists + CNTL(nlmp));
229 	lmc->lc_tail = (Rt_map *)PREV(nlmp);
230 	if (CNTL(nlmp) == ALO_DATA)
231 		lml->lm_tail = (Rt_map *)PREV(nlmp);
232 	lml->lm_obj--;
233 
234 	PREV(nlmp) = PREV(olmp);
235 	NEXT(nlmp) = NEXT(olmp);
236 	HANDLES(nlmp) = HANDLES(olmp);
237 	GROUPS(nlmp) = GROUPS(olmp);
238 	STDEV(nlmp) = STDEV(olmp);
239 	STINO(nlmp) = STINO(olmp);
240 
241 	FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
242 	FLAGS1(nlmp) |= FLAGS1(olmp);
243 	MODE(nlmp) |= MODE(olmp);
244 
245 	NAME(nlmp) = NAME(olmp);
246 	PATHNAME(nlmp) = PATHNAME(olmp);
247 	ORIGNAME(nlmp) = ORIGNAME(olmp);
248 	DIRSZ(nlmp) = DIRSZ(olmp);
249 
250 	ofl_cleanup(ofl);
251 	free(olmp->rt_priv);
252 	(void) memcpy(olmp, nlmp, sizeof (Rt_map));
253 	free(nlmp);
254 	nlmp = olmp;
255 	olmp = 0;
256 
257 	/*
258 	 * Now that we've allocated our permanent Rt_map structure, expand the
259 	 * PATHNAME() and insert it into the FullpathNode AVL tree
260 	 */
261 	if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
262 		(void) fullpath(nlmp, 0);
263 	if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
264 		return (0);
265 
266 	/*
267 	 * If we're being audited tell the audit library of the file we've just
268 	 * opened.
269 	 */
270 	if ((lml->lm_tflags | FLAGS1(nlmp)) & LML_TFLG_AUD_MASK) {
271 		if (audit_objopen(lmp, lmp) == 0)
272 			return (0);
273 	}
274 	return (nlmp);
275 }
276