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