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