xref: /titanic_50/usr/src/cmd/sgs/rtld/common/object.c (revision 3db3491215579980a91e230cf21b20608fbb8259)
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 2008 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 = 0;
44 
45 static Ehdr	dehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
46 			    M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
47 
48 /*
49  * Process a relocatable object.  The static object link map pointer is used as
50  * a flag to determine whether a concatenation is already in progress (ie. an
51  * LD_PRELOAD may specify a list of objects).  The link map returned simply
52  * specifies an `object' flag which the caller can interpret and thus call
53  * elf_obj_fini() to complete the concatenation.
54  */
55 static Rt_map *
56 elf_obj_init(Lm_list *lml, Aliste lmco, const char *name)
57 {
58 	Ofl_desc *	ofl;
59 
60 	/*
61 	 * Initialize an output file descriptor and the entrance criteria.
62 	 */
63 	if ((ofl = (Ofl_desc *)calloc(sizeof (Ofl_desc), 1)) == 0)
64 		return (0);
65 
66 	ofl->ofl_dehdr = &dehdr;
67 
68 	ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP);
69 	ofl->ofl_flags1 = (FLG_OF1_RELDYN | FLG_OF1_TEXTOFF | FLG_OF1_MEMORY);
70 	ofl->ofl_lml = lml;
71 
72 	/*
73 	 * As ent_setup() will effectively lazy load the necessary support
74 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
75 	 */
76 	if (elf_rtld_load() == 0)
77 		return (0);
78 
79 	/*
80 	 * Configure libld.so to process objects of the desired target
81 	 * type (this is the first call to libld.so, which will effectively
82 	 * lazyload it).
83 	 */
84 	if (ld_init_target(lml, M_MACH) != 0)
85 		return (0);
86 
87 	/*
88 	 * Obtain a generic set of entrance criteria
89 	 */
90 	if (ld_ent_setup(ofl, syspagsz) == S_ERROR)
91 		return (0);
92 
93 	/*
94 	 * Generate a link map place holder and use the `rt_priv' element to
95 	 * maintain the output file descriptor.
96 	 */
97 	if ((olmp = (Rt_map *)calloc(sizeof (Rt_map), 1)) == 0)
98 		return (0);
99 
100 	DBG_CALL(Dbg_file_elf(lml, name, 0, 0, 0, 0, lml->lm_lmidstr, lmco));
101 	FLAGS(olmp) |= FLG_RT_OBJECT;
102 	olmp->rt_priv = (void *)ofl;
103 
104 	/*
105 	 * Initialize string tables.
106 	 */
107 	if (ld_init_strings(ofl) == S_ERROR)
108 		return (0);
109 
110 	/*
111 	 * Assign the output file name to be the initial object that got us
112 	 * here.  This name is being used for diagnostic purposes only as we
113 	 * don't actually generate an output file unless debugging is enabled.
114 	 */
115 	ofl->ofl_name = name;
116 	ORIGNAME(olmp) = PATHNAME(olmp) = NAME(olmp) = (char *)name;
117 	LIST(olmp) = lml;
118 
119 	lm_append(lml, lmco, olmp);
120 	return (olmp);
121 }
122 
123 /*
124  * Initial processing of a relocatable object.  If this is the first object
125  * encountered we need to initialize some structures, then simply call the
126  * link-edit functionality to provide the initial processing of the file (ie.
127  * reads in sections and symbols, performs symbol resolution if more that one
128  * object file have been specified, and assigns input sections to output
129  * sections).
130  */
131 Rt_map *
132 elf_obj_file(Lm_list *lml, Aliste lmco, const char *name, int fd)
133 {
134 	Rej_desc	rej;
135 
136 	/*
137 	 * If this is the first relocatable object (LD_PRELOAD could provide a
138 	 * list of objects), initialize an input file descriptor and a link map.
139 	 */
140 	if (!olmp) {
141 		/*
142 		 * Load the link-editor library.
143 		 */
144 		if ((olmp = elf_obj_init(lml, lmco, name)) == 0)
145 			return (0);
146 	}
147 
148 	/*
149 	 * Proceed to process the input file.
150 	 */
151 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
152 	if (ld_process_open(name, name, &fd, (Ofl_desc *)olmp->rt_priv,
153 	    NULL, &rej) == (Ifl_desc *)S_ERROR)
154 		return (0);
155 	return (olmp);
156 }
157 
158 /*
159  * Finish relocatable object processing.  Having already initially processed one
160  * or more objects, complete the generation of a shared object image by calling
161  * the appropriate link-edit functionality (refer to sgs/ld/common/main.c).
162  */
163 Rt_map *
164 elf_obj_fini(Lm_list *lml, Rt_map *lmp, int *in_nfavl)
165 {
166 	Ofl_desc	*ofl = (Ofl_desc *)lmp->rt_priv;
167 	Rt_map		*nlmp;
168 	Addr		etext;
169 	Ehdr		*ehdr;
170 	Phdr		*phdr;
171 	Mmap		*mmaps;
172 	uint_t		phnum, mmapcnt;
173 	Lm_cntl 	*lmc;
174 
175 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
176 
177 	if (ld_reloc_init(ofl) == S_ERROR)
178 		return (0);
179 	if (ld_sym_validate(ofl) == S_ERROR)
180 		return (0);
181 	if (ld_make_sections(ofl) == S_ERROR)
182 		return (0);
183 	if (ld_create_outfile(ofl) == S_ERROR)
184 		return (0);
185 	if ((etext = ld_update_outfile(ofl)) == (Addr)S_ERROR)
186 		return (0);
187 	if (ld_reloc_process(ofl) == S_ERROR)
188 		return (0);
189 
190 	/*
191 	 * At this point we have a memory image of the shared object.  The link
192 	 * editor would normally simply write this to the required output file.
193 	 * If we're debugging generate a standard temporary output file.
194 	 */
195 	DBG_CALL(Dbg_file_output(ofl));
196 
197 	/*
198 	 * Allocate a mapping array to retain mapped segment information.
199 	 */
200 	ehdr = ofl->ofl_nehdr;
201 	phdr = ofl->ofl_phdr;
202 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
203 		return (0);
204 	for (mmapcnt = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
205 		if (phdr[phnum].p_type != PT_LOAD)
206 			continue;
207 
208 		mmaps[mmapcnt].m_vaddr = (caddr_t)
209 		    (phdr[phnum].p_vaddr + (ulong_t)ehdr);
210 		mmaps[mmapcnt].m_msize = phdr[phnum].p_memsz;
211 		mmaps[mmapcnt].m_fsize = phdr[phnum].p_filesz;
212 		mmaps[mmapcnt].m_perm = (PROT_READ | PROT_WRITE | PROT_EXEC);
213 		mmapcnt++;
214 	}
215 
216 	/*
217 	 * Generate a new link map representing the memory image created.
218 	 */
219 	if ((nlmp = elf_new_lm(lml, ofl->ofl_name, ofl->ofl_name,
220 	    ofl->ofl_osdynamic->os_outdata->d_buf, (ulong_t)ehdr,
221 	    (ulong_t)ehdr + etext, CNTL(olmp), (ulong_t)ofl->ofl_size,
222 	    0, 0, 0, mmaps, mmapcnt, in_nfavl)) == 0)
223 		return (0);
224 
225 	/*
226 	 * Remove this link map from the end of the link map list and copy its
227 	 * contents into the link map originally created for this file (we copy
228 	 * the contents rather than manipulate the link map pointers as parts
229 	 * of the dlopen code have remembered the original link map address).
230 	 */
231 	NEXT(PREV_RT_MAP(nlmp)) = 0;
232 	/* LINTED */
233 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
234 	lmc->lc_tail = PREV_RT_MAP(nlmp);
235 	if (CNTL(nlmp) == ALIST_OFF_DATA)
236 		lml->lm_tail = PREV_RT_MAP(nlmp);
237 	lml->lm_obj--;
238 
239 	PREV(nlmp) = PREV(olmp);
240 	NEXT(nlmp) = NEXT(olmp);
241 	HANDLES(nlmp) = HANDLES(olmp);
242 	GROUPS(nlmp) = GROUPS(olmp);
243 	STDEV(nlmp) = STDEV(olmp);
244 	STINO(nlmp) = STINO(olmp);
245 
246 	FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
247 	FLAGS1(nlmp) |= FLAGS1(olmp);
248 	MODE(nlmp) |= MODE(olmp);
249 
250 	NAME(nlmp) = NAME(olmp);
251 	PATHNAME(nlmp) = PATHNAME(olmp);
252 	ORIGNAME(nlmp) = ORIGNAME(olmp);
253 	DIRSZ(nlmp) = DIRSZ(olmp);
254 
255 	ld_ofl_cleanup(ofl);
256 	free(olmp->rt_priv);
257 	(void) memcpy(olmp, nlmp, sizeof (Rt_map));
258 	free(nlmp);
259 	nlmp = olmp;
260 	olmp = 0;
261 
262 	/*
263 	 * Now that we've allocated our permanent Rt_map structure, expand the
264 	 * PATHNAME() and insert it into the FullpathNode AVL tree
265 	 */
266 	if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
267 		(void) fullpath(nlmp, 0);
268 	if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
269 		return (0);
270 
271 	/*
272 	 * If we're being audited tell the audit library of the file we've just
273 	 * opened.
274 	 */
275 	if ((lml->lm_tflags | FLAGS1(nlmp)) & LML_TFLG_AUD_MASK) {
276 		if (audit_objopen(lmp, lmp) == 0)
277 			return (0);
278 	}
279 	return (nlmp);
280 }
281