xref: /titanic_44/usr/src/cmd/sgs/rtld/i386/i386_elf.c (revision e79c98e6c943cb3032f272714ff4ce6137d40394)
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 (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * x86 machine dependent and ELF file class dependent functions.
33  * Contains routines for performing function binding and symbol relocations.
34  */
35 #include	"_synonyms.h"
36 
37 #include	<stdio.h>
38 #include	<sys/elf.h>
39 #include	<sys/elf_386.h>
40 #include	<sys/mman.h>
41 #include	<dlfcn.h>
42 #include	<synch.h>
43 #include	<string.h>
44 #include	<debug.h>
45 #include	<reloc.h>
46 #include	<conv.h>
47 #include	"_rtld.h"
48 #include	"_audit.h"
49 #include	"_elf.h"
50 #include	"msg.h"
51 
52 
53 extern void	elf_rtbndr(Rt_map *, ulong_t, caddr_t);
54 
55 int
56 elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr)
57 {
58 	/*
59 	 * Check machine type and flags.
60 	 */
61 	if (ehdr->e_flags != 0) {
62 		rej->rej_type = SGS_REJ_BADFLAG;
63 		rej->rej_info = (uint_t)ehdr->e_flags;
64 		return (0);
65 	}
66 	return (1);
67 }
68 
69 void
70 ldso_plt_init(Rt_map * lmp)
71 {
72 	/*
73 	 * There is no need to analyze ld.so because we don't map in any of
74 	 * its dependencies.  However we may map these dependencies in later
75 	 * (as if ld.so had dlopened them), so initialize the plt and the
76 	 * permission information.
77 	 */
78 	if (PLTGOT(lmp))
79 		elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp);
80 }
81 
82 static const uchar_t dyn_plt_template[] = {
83 /* 0x00 */  0x55,				/* pushl %ebp */
84 /* 0x01 */  0x8b, 0xec,				/* movl %esp, %ebp */
85 /* 0x03 */  0x68, 0x00, 0x00, 0x00, 0x00,	/* pushl trace_fields */
86 /* 0x08 */  0xe9, 0xfc, 0xff, 0xff, 0xff, 0xff	/* jmp  elf_plt_trace */
87 };
88 int	dyn_plt_ent_size = sizeof (dyn_plt_template);
89 
90 /*
91  * the dynamic plt entry is:
92  *
93  *	pushl	%ebp
94  *	movl	%esp, %ebp
95  *	pushl	tfp
96  *	jmp	elf_plt_trace
97  * dyn_data:
98  *	.align  4
99  *	uintptr_t	reflmp
100  *	uintptr_t	deflmp
101  *	uint_t		symndx
102  *	uint_t		sb_flags
103  *	Sym		symdef
104  */
105 static caddr_t
106 elf_plt_trace_write(uint_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym,
107     uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail)
108 {
109 	extern int	elf_plt_trace();
110 	ulong_t		got_entry;
111 	uchar_t		*dyn_plt;
112 	uintptr_t	*dyndata;
113 
114 	/*
115 	 * We only need to add the glue code if there is an auditing
116 	 * library that is interested in this binding.
117 	 */
118 	dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts +
119 	    (pltndx * dyn_plt_ent_size));
120 
121 	/*
122 	 * Have we initialized this dynamic plt entry yet?  If we haven't do it
123 	 * now.  Otherwise this function has been called before, but from a
124 	 * different plt (ie. from another shared object).  In that case
125 	 * we just set the plt to point to the new dyn_plt.
126 	 */
127 	if (*dyn_plt == 0) {
128 		Sym	*symp;
129 		Word	symvalue;
130 		Lm_list	*lml = LIST(rlmp);
131 
132 		(void) memcpy((void *)dyn_plt, dyn_plt_template,
133 		    sizeof (dyn_plt_template));
134 		dyndata = (uintptr_t *)((uintptr_t)dyn_plt +
135 		    ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN));
136 
137 		/*
138 		 * relocate:
139 		 *	pushl	dyn_data
140 		 */
141 		symvalue = (Word)dyndata;
142 		if (do_reloc_rtld(R_386_32, &dyn_plt[4], &symvalue,
143 		    MSG_ORIG(MSG_SYM_LADYNDATA),
144 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
145 			*fail = 1;
146 			return (0);
147 		}
148 
149 		/*
150 		 * jmps are relative, so I need to figure out the relative
151 		 * address to elf_plt_trace.
152 		 *
153 		 * relocating:
154 		 *	jmp	elf_plt_trace
155 		 */
156 		symvalue = (ulong_t)(elf_plt_trace) - (ulong_t)(dyn_plt + 9);
157 		if (do_reloc_rtld(R_386_PC32, &dyn_plt[9], &symvalue,
158 		    MSG_ORIG(MSG_SYM_ELFPLTTRACE),
159 		    MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) {
160 			*fail = 1;
161 			return (0);
162 		}
163 
164 		*dyndata++ = (uintptr_t)rlmp;
165 		*dyndata++ = (uintptr_t)dlmp;
166 		*dyndata++ = (uint_t)symndx;
167 		*dyndata++ = (uint_t)sb_flags;
168 		symp = (Sym *)dyndata;
169 		*symp = *sym;
170 		symp->st_name += (Word)STRTAB(dlmp);
171 		symp->st_value = (Addr)to;
172 	}
173 
174 	got_entry = (ulong_t)roffset;
175 	*(ulong_t *)got_entry = (ulong_t)dyn_plt;
176 	return ((caddr_t)dyn_plt);
177 }
178 
179 
180 /*
181  * Function binding routine - invoked on the first call to a function through
182  * the procedure linkage table;
183  * passes first through an assembly language interface.
184  *
185  * Takes the offset into the relocation table of the associated
186  * relocation entry and the address of the link map (rt_private_map struct)
187  * for the entry.
188  *
189  * Returns the address of the function referenced after re-writing the PLT
190  * entry to invoke the function directly.
191  *
192  * On error, causes process to terminate with a signal.
193  */
194 ulong_t
195 elf_bndr(Rt_map *lmp, ulong_t reloff, caddr_t from)
196 {
197 	Rt_map		*nlmp, *llmp;
198 	ulong_t		addr, symval, rsymndx;
199 	char		*name;
200 	Rel		*rptr;
201 	Sym		*rsym, *nsym;
202 	uint_t		binfo, sb_flags = 0, dbg_class;
203 	Slookup		sl;
204 	int		entry, lmflags;
205 	Lm_list		*lml;
206 
207 	/*
208 	 * For compatibility with libthread (TI_VERSION 1) we track the entry
209 	 * value.  A zero value indicates we have recursed into ld.so.1 to
210 	 * further process a locking request.  Under this recursion we disable
211 	 * tsort and cleanup activities.
212 	 */
213 	entry = enter();
214 
215 	lml = LIST(lmp);
216 	if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) {
217 		dbg_class = dbg_desc->d_class;
218 		dbg_desc->d_class = 0;
219 	}
220 
221 	/*
222 	 * Perform some basic sanity checks.  If we didn't get a load map or
223 	 * the relocation offset is invalid then its possible someone has walked
224 	 * over the .got entries or jumped to plt0 out of the blue.
225 	 */
226 	if (!lmp || ((reloff % sizeof (Rel)) != 0)) {
227 		Conv_inv_buf_t inv_buf;
228 
229 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF),
230 		    conv_reloc_386_type(R_386_JMP_SLOT, 0, &inv_buf),
231 		    EC_NATPTR(lmp), EC_XWORD(reloff), EC_NATPTR(from));
232 		rtldexit(lml, 1);
233 	}
234 
235 	/*
236 	 * Use relocation entry to get symbol table entry and symbol name.
237 	 */
238 	addr = (ulong_t)JMPREL(lmp);
239 	rptr = (Rel *)(addr + reloff);
240 	rsymndx = ELF_R_SYM(rptr->r_info);
241 	rsym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
242 	name = (char *)(STRTAB(lmp) + rsym->st_name);
243 
244 	/*
245 	 * Determine the last link-map of this list, this'll be the starting
246 	 * point for any tsort() processing.
247 	 */
248 	llmp = lml->lm_tail;
249 
250 	/*
251 	 * Find definition for symbol.
252 	 */
253 	sl.sl_name = name;
254 	sl.sl_cmap = lmp;
255 	sl.sl_imap = lml->lm_head;
256 	sl.sl_hash = 0;
257 	sl.sl_rsymndx = rsymndx;
258 	sl.sl_rsym = rsym;
259 	sl.sl_flags = LKUP_DEFT;
260 
261 	if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) {
262 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
263 		    demangle(name));
264 		rtldexit(lml, 1);
265 	}
266 
267 	symval = nsym->st_value;
268 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
269 	    (nsym->st_shndx != SHN_ABS))
270 		symval += ADDR(nlmp);
271 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
272 		/*
273 		 * Record that this new link map is now bound to the caller.
274 		 */
275 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
276 			rtldexit(lml, 1);
277 	}
278 
279 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) {
280 		uint_t	symndx = (((uintptr_t)nsym -
281 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
282 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
283 		    &sb_flags);
284 	}
285 
286 	if (!(rtld_flags & RT_FL_NOBIND)) {
287 		addr = rptr->r_offset;
288 		if (!(FLAGS(lmp) & FLG_RT_FIXED))
289 			addr += ADDR(lmp);
290 		if (((lml->lm_tflags | FLAGS1(lmp)) &
291 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
292 		    AUDINFO(lmp)->ai_dynplts) {
293 			int	fail = 0;
294 			uint_t	pltndx = reloff / sizeof (Rel);
295 			uint_t	symndx = (((uintptr_t)nsym -
296 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
297 
298 			symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp,
299 			    nsym, symndx, pltndx, (caddr_t)symval, sb_flags,
300 			    &fail);
301 			if (fail)
302 				rtldexit(lml, 1);
303 		} else {
304 			/*
305 			 * Write standard PLT entry to jump directly
306 			 * to newly bound function.
307 			 */
308 			*(ulong_t *)addr = symval;
309 		}
310 	}
311 
312 	/*
313 	 * Print binding information and rebuild PLT entry.
314 	 */
315 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
316 	    (Xword)(reloff / sizeof (Rel)), PLT_T_FULL, nlmp, (Addr)symval,
317 	    nsym->st_value, name, binfo));
318 
319 	/*
320 	 * Complete any processing for newly loaded objects.  Note we don't
321 	 * know exactly where any new objects are loaded (we know the object
322 	 * that supplied the symbol, but others may have been loaded lazily as
323 	 * we searched for the symbol), so sorting starts from the last
324 	 * link-map know on entry to this routine.
325 	 */
326 	if (entry)
327 		load_completion(llmp);
328 
329 	/*
330 	 * Some operations like dldump() or dlopen()'ing a relocatable object
331 	 * result in objects being loaded on rtld's link-map, make sure these
332 	 * objects are initialized also.
333 	 */
334 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
335 		load_completion(nlmp);
336 
337 	/*
338 	 * If the object we've bound to is in the process of being initialized
339 	 * by another thread, determine whether we should block.
340 	 */
341 	is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL);
342 
343 	/*
344 	 * Make sure the object to which we've bound has had it's .init fired.
345 	 * Cleanup before return to user code.
346 	 */
347 	if (entry) {
348 		is_dep_init(nlmp, lmp);
349 		leave(lml);
350 	}
351 
352 	if (lmflags & LML_FLG_RTLDLM)
353 		dbg_desc->d_class = dbg_class;
354 
355 	return (symval);
356 }
357 
358 
359 /*
360  * When the relocation loop realizes that it's dealing with relative
361  * relocations in a shared object, it breaks into this tighter loop
362  * as an optimization.
363  */
364 ulong_t
365 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
366     ulong_t basebgn, ulong_t etext, ulong_t emap)
367 {
368 	ulong_t roffset = ((Rel *)relbgn)->r_offset;
369 	char rtype;
370 
371 	do {
372 		roffset += basebgn;
373 
374 		/*
375 		 * If this relocation is against an address not mapped in,
376 		 * then break out of the relative relocation loop, falling
377 		 * back on the main relocation loop.
378 		 */
379 		if (roffset < etext || roffset > emap)
380 			break;
381 
382 		/*
383 		 * Perform the actual relocation.
384 		 */
385 		*((ulong_t *)roffset) += basebgn;
386 
387 		relbgn += relsiz;
388 
389 		if (relbgn >= relend)
390 			break;
391 
392 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
393 		roffset = ((Rel *)relbgn)->r_offset;
394 
395 	} while (rtype == R_386_RELATIVE);
396 
397 	return (relbgn);
398 }
399 
400 /*
401  * This is the tightest loop for RELATIVE relocations for those
402  * objects built with the DT_RELACOUNT .dynamic entry.
403  */
404 ulong_t
405 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
406     ulong_t basebgn)
407 {
408 	ulong_t roffset = ((Rel *) relbgn)->r_offset;
409 
410 	for (; relacount; relacount--) {
411 		roffset += basebgn;
412 
413 		/*
414 		 * Perform the actual relocation.
415 		 */
416 		*((ulong_t *)roffset) += basebgn;
417 
418 		relbgn += relsiz;
419 
420 		roffset = ((Rel *)relbgn)->r_offset;
421 
422 	}
423 
424 	return (relbgn);
425 }
426 
427 /*
428  * Read and process the relocations for one link object, we assume all
429  * relocation sections for loadable segments are stored contiguously in
430  * the file.
431  */
432 int
433 elf_reloc(Rt_map *lmp, uint_t plt)
434 {
435 	ulong_t		relbgn, relend, relsiz, basebgn;
436 	ulong_t		pltbgn, pltend, _pltbgn, _pltend;
437 	ulong_t		roffset, rsymndx, psymndx = 0, etext  = ETEXT(lmp);
438 	ulong_t		emap, dsymndx;
439 	uchar_t		rtype;
440 	long		value, pvalue;
441 	Sym		*symref, *psymref, *symdef, *psymdef;
442 	char		*name, *pname;
443 	Rt_map		*_lmp, *plmp;
444 	int		textrel = 0, ret = 1, noplt = 0;
445 	int		relacount = RELACOUNT(lmp), plthint = 0;
446 	Rel		*rel;
447 	uint_t		binfo, pbinfo;
448 	Alist		*bound = 0;
449 
450 	/*
451 	 * Although only necessary for lazy binding, initialize the first
452 	 * global offset entry to go to elf_rtbndr().  dbx(1) seems
453 	 * to find this useful.
454 	 */
455 	if ((plt == 0) && PLTGOT(lmp)) {
456 		if ((ulong_t)PLTGOT(lmp) < etext) {
457 			if (elf_set_prot(lmp, PROT_WRITE) == 0)
458 				return (0);
459 			textrel = 1;
460 		}
461 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
462 	}
463 
464 	/*
465 	 * Initialize the plt start and end addresses.
466 	 */
467 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
468 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
469 
470 
471 	relsiz = (ulong_t)(RELENT(lmp));
472 	basebgn = ADDR(lmp);
473 	emap = ADDR(lmp) + MSIZE(lmp);
474 
475 	if (PLTRELSZ(lmp))
476 		plthint = PLTRELSZ(lmp) / relsiz;
477 
478 	/*
479 	 * If we've been called upon to promote an RTLD_LAZY object to an
480 	 * RTLD_NOW then we're only interested in scaning the .plt table.
481 	 * An uninitialized .plt is the case where the associated got entry
482 	 * points back to the plt itself.  Determine the range of the real .plt
483 	 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol.
484 	 */
485 	if (plt) {
486 		Slookup	sl;
487 
488 		relbgn = pltbgn;
489 		relend = pltend;
490 		if (!relbgn || (relbgn == relend))
491 			return (1);
492 
493 		sl.sl_name = MSG_ORIG(MSG_SYM_PLT);
494 		sl.sl_cmap = lmp;
495 		sl.sl_imap = lmp;
496 		sl.sl_hash = elf_hash(MSG_ORIG(MSG_SYM_PLT));
497 		sl.sl_rsymndx = 0;
498 		sl.sl_rsym = 0;
499 		sl.sl_flags = LKUP_DEFT;
500 
501 		if ((symdef = elf_find_sym(&sl, &_lmp, &binfo)) == 0)
502 			return (1);
503 
504 		_pltbgn = symdef->st_value;
505 		if (!(FLAGS(lmp) & FLG_RT_FIXED) &&
506 		    (symdef->st_shndx != SHN_ABS))
507 			_pltbgn += basebgn;
508 		_pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) *
509 		    M_PLT_ENTSIZE) + M_PLT_RESERVSZ;
510 
511 	} else {
512 		/*
513 		 * The relocation sections appear to the run-time linker as a
514 		 * single table.  Determine the address of the beginning and end
515 		 * of this table.  There are two different interpretations of
516 		 * the ABI at this point:
517 		 *
518 		 *   o	The REL table and its associated RELSZ indicate the
519 		 *	concatenation of *all* relocation sections (this is the
520 		 *	model our link-editor constructs).
521 		 *
522 		 *   o	The REL table and its associated RELSZ indicate the
523 		 *	concatenation of all *but* the .plt relocations.  These
524 		 *	relocations are specified individually by the JMPREL and
525 		 *	PLTRELSZ entries.
526 		 *
527 		 * Determine from our knowledege of the relocation range and
528 		 * .plt range, the range of the total relocation table.  Note
529 		 * that one other ABI assumption seems to be that the .plt
530 		 * relocations always follow any other relocations, the
531 		 * following range checking drops that assumption.
532 		 */
533 		relbgn = (ulong_t)(REL(lmp));
534 		relend = relbgn + (ulong_t)(RELSZ(lmp));
535 		if (pltbgn) {
536 			if (!relbgn || (relbgn > pltbgn))
537 				relbgn = pltbgn;
538 			if (!relbgn || (relend < pltend))
539 				relend = pltend;
540 		}
541 	}
542 	if (!relbgn || (relbgn == relend)) {
543 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
544 		return (1);
545 	}
546 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
547 
548 	/*
549 	 * If we're processing a dynamic executable in lazy mode there is no
550 	 * need to scan the .rel.plt table, however if we're processing a shared
551 	 * object in lazy mode the .got addresses associated to each .plt must
552 	 * be relocated to reflect the location of the shared object.
553 	 */
554 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) &&
555 	    (FLAGS(lmp) & FLG_RT_FIXED))
556 		noplt = 1;
557 
558 	/*
559 	 * Loop through relocations.
560 	 */
561 	while (relbgn < relend) {
562 		uint_t	sb_flags = 0;
563 
564 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
565 
566 		/*
567 		 * If this is a RELATIVE relocation in a shared object (the
568 		 * common case), and if we are not debugging, then jump into a
569 		 * tighter relocation loop (elf_reloc_relative).  Only make the
570 		 * jump if we've been given a hint on the number of relocations.
571 		 */
572 		if ((rtype == R_386_RELATIVE) &&
573 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
574 			/*
575 			 * It's possible that the relative relocation block
576 			 * has relocations against the text segment as well
577 			 * as the data segment.  Since our optimized relocation
578 			 * engine does not check which segment the relocation
579 			 * is against - just mprotect it now if it's been
580 			 * marked as containing TEXTREL's.
581 			 */
582 			if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) {
583 				if (elf_set_prot(lmp, PROT_WRITE) == 0) {
584 					ret = 0;
585 					break;
586 				}
587 				textrel = 1;
588 			}
589 
590 			if (relacount) {
591 				relbgn = elf_reloc_relacount(relbgn, relacount,
592 				    relsiz, basebgn);
593 				relacount = 0;
594 			} else {
595 				relbgn = elf_reloc_relative(relbgn, relend,
596 				    relsiz, basebgn, etext, emap);
597 			}
598 			if (relbgn >= relend)
599 				break;
600 			rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
601 		}
602 
603 		roffset = ((Rel *)relbgn)->r_offset;
604 
605 		/*
606 		 * If this is a shared object, add the base address to offset.
607 		 */
608 		if (!(FLAGS(lmp) & FLG_RT_FIXED)) {
609 
610 			/*
611 			 * If we're processing lazy bindings, we have to step
612 			 * through the plt entries and add the base address
613 			 * to the corresponding got entry.
614 			 */
615 			if (plthint && (plt == 0) &&
616 			    (rtype == R_386_JMP_SLOT) &&
617 			    ((MODE(lmp) & RTLD_NOW) == 0)) {
618 				relbgn = elf_reloc_relacount(relbgn,
619 				    plthint, relsiz, basebgn);
620 				plthint = 0;
621 				continue;
622 			}
623 			roffset += basebgn;
624 		}
625 
626 		rsymndx = ELF_R_SYM(((Rel *)relbgn)->r_info);
627 		rel = (Rel *)relbgn;
628 		relbgn += relsiz;
629 
630 		/*
631 		 * Optimizations.
632 		 */
633 		if (rtype == R_386_NONE)
634 			continue;
635 		if (noplt && ((ulong_t)rel >= pltbgn) &&
636 		    ((ulong_t)rel < pltend)) {
637 			relbgn = pltend;
638 			continue;
639 		}
640 
641 		/*
642 		 * If we're promoting plts determine if this one has already
643 		 * been written.
644 		 */
645 		if (plt) {
646 			if ((*(ulong_t *)roffset < _pltbgn) ||
647 			    (*(ulong_t *)roffset > _pltend))
648 				continue;
649 		}
650 
651 		/*
652 		 * If this relocation is not against part of the image
653 		 * mapped into memory we skip it.
654 		 */
655 		if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) +
656 		    MSIZE(lmp)))) {
657 			elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
658 			    rsymndx);
659 			continue;
660 		}
661 
662 		binfo = 0;
663 		/*
664 		 * If a symbol index is specified then get the symbol table
665 		 * entry, locate the symbol definition, and determine its
666 		 * address.
667 		 */
668 		if (rsymndx) {
669 			/*
670 			 * Get the local symbol table entry.
671 			 */
672 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
673 			    (rsymndx * SYMENT(lmp)));
674 
675 			/*
676 			 * If this is a local symbol, just use the base address.
677 			 * (we should have no local relocations in the
678 			 * executable).
679 			 */
680 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
681 				value = basebgn;
682 				name = (char *)0;
683 
684 				/*
685 				 * Special case TLS relocations.
686 				 */
687 				if (rtype == R_386_TLS_DTPMOD32) {
688 					/*
689 					 * Use the TLS modid.
690 					 */
691 					value = TLSMODID(lmp);
692 
693 				} else if (rtype == R_386_TLS_TPOFF) {
694 					if ((value = elf_static_tls(lmp, symref,
695 					    rel, rtype, 0, roffset, 0)) == 0) {
696 						ret = 0;
697 						break;
698 					}
699 				}
700 			} else {
701 				/*
702 				 * If the symbol index is equal to the previous
703 				 * symbol index relocation we processed then
704 				 * reuse the previous values. (Note that there
705 				 * have been cases where a relocation exists
706 				 * against a copy relocation symbol, our ld(1)
707 				 * should optimize this away, but make sure we
708 				 * don't use the same symbol information should
709 				 * this case exist).
710 				 */
711 				if ((rsymndx == psymndx) &&
712 				    (rtype != R_386_COPY)) {
713 					/* LINTED */
714 					if (psymdef == 0) {
715 						DBG_CALL(Dbg_bind_weak(lmp,
716 						    (Addr)roffset, (Addr)
717 						    (roffset - basebgn), name));
718 						continue;
719 					}
720 					/* LINTED */
721 					value = pvalue;
722 					/* LINTED */
723 					name = pname;
724 					/* LINTED */
725 					symdef = psymdef;
726 					/* LINTED */
727 					symref = psymref;
728 					/* LINTED */
729 					_lmp = plmp;
730 					/* LINTED */
731 					binfo = pbinfo;
732 
733 					if ((LIST(_lmp)->lm_tflags |
734 					    FLAGS1(_lmp)) &
735 					    LML_TFLG_AUD_SYMBIND) {
736 						value = audit_symbind(lmp, _lmp,
737 						    /* LINTED */
738 						    symdef, dsymndx, value,
739 						    &sb_flags);
740 					}
741 				} else {
742 					Slookup		sl;
743 
744 					/*
745 					 * Lookup the symbol definition.
746 					 */
747 					name = (char *)(STRTAB(lmp) +
748 					    symref->st_name);
749 
750 					sl.sl_name = name;
751 					sl.sl_cmap = lmp;
752 					sl.sl_imap = 0;
753 					sl.sl_hash = 0;
754 					sl.sl_rsymndx = rsymndx;
755 					sl.sl_rsym = symref;
756 					sl.sl_rtype = rtype;
757 					sl.sl_flags = LKUP_STDRELOC;
758 
759 					symdef = lookup_sym(&sl, &_lmp, &binfo);
760 
761 					/*
762 					 * If the symbol is not found and the
763 					 * reference was not to a weak symbol,
764 					 * report an error.  Weak references
765 					 * may be unresolved.
766 					 * chkmsg: MSG_INTL(MSG_LDD_SYM_NFOUND)
767 					 */
768 					/* BEGIN CSTYLED */
769 					if (symdef == 0) {
770 					    Lm_list	*lml = LIST(lmp);
771 
772 					    if (sl.sl_bind != STB_WEAK) {
773 						if (lml->lm_flags &
774 						    LML_FLG_IGNRELERR) {
775 						    continue;
776 						} else if (lml->lm_flags &
777 						    LML_FLG_TRC_WARN) {
778 						    (void) printf(MSG_INTL(
779 							MSG_LDD_SYM_NFOUND),
780 							demangle(name),
781 							NAME(lmp));
782 						    continue;
783 						} else {
784 						    DBG_CALL(Dbg_reloc_in(lml,
785 							ELF_DBG_RTLD, M_MACH,
786 							M_REL_SHT_TYPE, rel,
787 							NULL, name));
788 						    eprintf(lml, ERR_FATAL,
789 							MSG_INTL(MSG_REL_NOSYM),
790 							NAME(lmp),
791 							demangle(name));
792 						    ret = 0;
793 						    break;
794 						}
795 					    } else {
796 						psymndx = rsymndx;
797 						psymdef = 0;
798 
799 						DBG_CALL(Dbg_bind_weak(lmp,
800 						    (Addr)roffset, (Addr)
801 						    (roffset - basebgn), name));
802 						continue;
803 					    }
804 					}
805 					/* END CSTYLED */
806 
807 					/*
808 					 * If symbol was found in an object
809 					 * other than the referencing object
810 					 * then record the binding.
811 					 */
812 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
813 					    FL1_RT_NOINIFIN) == 0)) {
814 						if (alist_test(&bound, _lmp,
815 						    sizeof (Rt_map *),
816 						    AL_CNT_RELBIND) == 0) {
817 							ret = 0;
818 							break;
819 						}
820 					}
821 
822 					/*
823 					 * Calculate the location of definition;
824 					 * symbol value plus base address of
825 					 * containing shared object.
826 					 */
827 					if (IS_SIZE(rtype))
828 						value = symdef->st_size;
829 					else
830 						value = symdef->st_value;
831 
832 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
833 					    !(IS_SIZE(rtype)) &&
834 					    (symdef->st_shndx != SHN_ABS) &&
835 					    (ELF_ST_TYPE(symdef->st_info) !=
836 					    STT_TLS))
837 						value += ADDR(_lmp);
838 
839 					/*
840 					 * Retain this symbol index and the
841 					 * value in case it can be used for the
842 					 * subsequent relocations.
843 					 */
844 					if (rtype != R_386_COPY) {
845 						psymndx = rsymndx;
846 						pvalue = value;
847 						pname = name;
848 						psymdef = symdef;
849 						psymref = symref;
850 						plmp = _lmp;
851 						pbinfo = binfo;
852 					}
853 					if ((LIST(_lmp)->lm_tflags |
854 					    FLAGS1(_lmp)) &
855 					    LML_TFLG_AUD_SYMBIND) {
856 						dsymndx = (((uintptr_t)symdef -
857 						    (uintptr_t)SYMTAB(_lmp)) /
858 						    SYMENT(_lmp));
859 						value = audit_symbind(lmp, _lmp,
860 						    symdef, dsymndx, value,
861 						    &sb_flags);
862 					}
863 				}
864 
865 				/*
866 				 * If relocation is PC-relative, subtract
867 				 * offset address.
868 				 */
869 				if (IS_PC_RELATIVE(rtype))
870 					value -= roffset;
871 
872 				/*
873 				 * Special case TLS relocations.
874 				 */
875 				if (rtype == R_386_TLS_DTPMOD32) {
876 					/*
877 					 * Relocation value is the TLS modid.
878 					 */
879 					value = TLSMODID(_lmp);
880 
881 				} else if (rtype == R_386_TLS_TPOFF) {
882 					if ((value = elf_static_tls(_lmp,
883 					    symdef, rel, rtype, name, roffset,
884 					    value)) == 0) {
885 						ret = 0;
886 						break;
887 					}
888 				}
889 			}
890 		} else {
891 			/*
892 			 * Special cases.
893 			 */
894 			if (rtype == R_386_TLS_DTPMOD32) {
895 				/*
896 				 * TLS relocation value is the TLS modid.
897 				 */
898 				value = TLSMODID(lmp);
899 			} else
900 				value = basebgn;
901 			name = (char *)0;
902 		}
903 
904 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
905 		    M_REL_SHT_TYPE, rel, NULL, name));
906 
907 		/*
908 		 * If this object has relocations in the text segment, turn
909 		 * off the write protect.
910 		 */
911 		if ((roffset < etext) && (textrel == 0)) {
912 			if (elf_set_prot(lmp, PROT_WRITE) == 0) {
913 				ret = 0;
914 				break;
915 			}
916 			textrel = 1;
917 		}
918 
919 		/*
920 		 * Call relocation routine to perform required relocation.
921 		 */
922 		switch (rtype) {
923 		case R_386_COPY:
924 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
925 			    symdef, _lmp, (const void *)value) == 0)
926 				ret = 0;
927 			break;
928 		case R_386_JMP_SLOT:
929 			if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) &
930 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
931 			    AUDINFO(lmp)->ai_dynplts) {
932 				int	fail = 0;
933 				int	pltndx = (((ulong_t)rel -
934 				    (uintptr_t)JMPREL(lmp)) / relsiz);
935 				int	symndx = (((uintptr_t)symdef -
936 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
937 
938 				(void) elf_plt_trace_write(roffset, lmp, _lmp,
939 				    symdef, symndx, pltndx, (caddr_t)value,
940 				    sb_flags, &fail);
941 				if (fail)
942 					ret = 0;
943 			} else {
944 				/*
945 				 * Write standard PLT entry to jump directly
946 				 * to newly bound function.
947 				 */
948 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
949 				    ELF_DBG_RTLD, (Xword)roffset,
950 				    (Xword)value));
951 				*(ulong_t *)roffset = value;
952 			}
953 			break;
954 		default:
955 			/*
956 			 * Write the relocation out.
957 			 */
958 			if (do_reloc_rtld(rtype, (uchar_t *)roffset,
959 			    (Word *)&value, name, NAME(lmp), LIST(lmp)) == 0)
960 				ret = 0;
961 
962 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
963 			    (Xword)roffset, (Xword)value));
964 		}
965 
966 		if ((ret == 0) &&
967 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
968 			break;
969 
970 		if (binfo) {
971 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
972 			    (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL,
973 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
974 		}
975 	}
976 
977 	return (relocate_finish(lmp, bound, textrel, ret));
978 }
979 
980 /*
981  * Initialize the first few got entries so that function calls go to
982  * elf_rtbndr:
983  *
984  *	GOT[GOT_XLINKMAP] =	the address of the link map
985  *	GOT[GOT_XRTLD] =	the address of rtbinder
986  */
987 void
988 elf_plt_init(void *got, caddr_t l)
989 {
990 	uint_t		*_got;
991 	/* LINTED */
992 	Rt_map		*lmp = (Rt_map *)l;
993 
994 	_got = (uint_t *)got + M_GOT_XLINKMAP;
995 	*_got = (uint_t)lmp;
996 	_got = (uint_t *)got + M_GOT_XRTLD;
997 	*_got = (uint_t)elf_rtbndr;
998 }
999 
1000 /*
1001  * For SVR4 Intel compatability.  USL uses /usr/lib/libc.so.1 as the run-time
1002  * linker, so the interpreter's address will differ from /usr/lib/ld.so.1.
1003  * Further, USL has special _iob[] and _ctype[] processing that makes up for the
1004  * fact that these arrays do not have associated copy relocations.  So we try
1005  * and make up for that here.  Any relocations found will be added to the global
1006  * copy relocation list and will be processed in setup().
1007  */
1008 static int
1009 _elf_copy_reloc(const char *name, Rt_map *rlmp, Rt_map *dlmp)
1010 {
1011 	Sym		*symref, *symdef;
1012 	caddr_t 	ref, def;
1013 	Rt_map		*_lmp;
1014 	Rel		rel;
1015 	Slookup		sl;
1016 	uint_t		binfo;
1017 
1018 	/*
1019 	 * Determine if the special symbol exists as a reference in the dynamic
1020 	 * executable, and that an associated definition exists in libc.so.1.
1021 	 */
1022 	sl.sl_name = name;
1023 	sl.sl_cmap = rlmp;
1024 	sl.sl_imap = rlmp;
1025 	sl.sl_hash = 0;
1026 	sl.sl_rsymndx = 0;
1027 	sl.sl_rsym = 0;
1028 	sl.sl_flags = LKUP_FIRST;
1029 
1030 	if ((symref = lookup_sym(&sl, &_lmp, &binfo)) == 0)
1031 		return (1);
1032 
1033 	sl.sl_imap = dlmp;
1034 	sl.sl_flags = LKUP_DEFT;
1035 
1036 	if ((symdef = lookup_sym(&sl, &_lmp, &binfo)) == 0)
1037 		return (1);
1038 	if (strcmp(NAME(_lmp), MSG_ORIG(MSG_PTH_LIBC)))
1039 		return (1);
1040 
1041 	/*
1042 	 * Determine the reference and definition addresses.
1043 	 */
1044 	ref = (void *)(symref->st_value);
1045 	if (!(FLAGS(rlmp) & FLG_RT_FIXED))
1046 		ref += ADDR(rlmp);
1047 	def = (void *)(symdef->st_value);
1048 	if (!(FLAGS(_lmp) & FLG_RT_FIXED))
1049 		def += ADDR(_lmp);
1050 
1051 	/*
1052 	 * Set up a relocation entry for debugging and call the generic copy
1053 	 * relocation function to provide symbol size error checking and to
1054 	 * record the copy relocation that must be performed.
1055 	 */
1056 	rel.r_offset = (Addr)ref;
1057 	rel.r_info = (Word)R_386_COPY;
1058 	DBG_CALL(Dbg_reloc_in(LIST(rlmp), ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE,
1059 	    &rel, NULL, name));
1060 
1061 	return (elf_copy_reloc((char *)name, symref, rlmp, (void *)ref, symdef,
1062 	    _lmp, (void *)def));
1063 }
1064 
1065 int
1066 elf_copy_gen(Rt_map *lmp)
1067 {
1068 	if (interp && ((ulong_t)interp->i_faddr !=
1069 	    r_debug.rtd_rdebug.r_ldbase) &&
1070 	    !(strcmp(interp->i_name, MSG_ORIG(MSG_PTH_LIBC)))) {
1071 
1072 		DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, 0,
1073 		    DBG_REL_START));
1074 
1075 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_CTYPE), lmp,
1076 		    (Rt_map *)NEXT(lmp)) == 0)
1077 			return (0);
1078 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_IOB), lmp,
1079 		    (Rt_map *)NEXT(lmp)) == 0)
1080 			return (0);
1081 	}
1082 	return (1);
1083 }
1084 
1085 /*
1086  * Plt writing interface to allow debugging initialization to be generic.
1087  */
1088 Pltbindtype
1089 /* ARGSUSED1 */
1090 elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
1091 	Xword pltndx)
1092 {
1093 	Rel		*rel = (Rel*)rptr;
1094 	uintptr_t	pltaddr;
1095 
1096 	pltaddr = addr + rel->r_offset;
1097 	*(ulong_t *)pltaddr = (ulong_t)symval;
1098 	DBG_CALL(pltcntfull++);
1099 	return (PLT_T_FULL);
1100 }
1101 
1102 /*
1103  * Provide a machine specific interface to the conversion routine.  By calling
1104  * the machine specific version, rather than the generic version, we insure that
1105  * the data tables/strings for all known machine versions aren't dragged into
1106  * ld.so.1.
1107  */
1108 const char *
1109 _conv_reloc_type(uint_t rel)
1110 {
1111 	static Conv_inv_buf_t inv_buf;
1112 
1113 	return (conv_reloc_386_type(rel, 0, &inv_buf));
1114 }
1115