xref: /titanic_52/usr/src/cmd/sgs/rtld/i386/i386_elf.c (revision d29b2c4438482eb00488be49a1f5d6835f455546)
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(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(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		*sym, *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 	sym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
242 	name = (char *)(STRTAB(lmp) + sym->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_flags = LKUP_DEFT;
259 
260 	if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) {
261 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
262 		    demangle(name));
263 		rtldexit(lml, 1);
264 	}
265 
266 	symval = nsym->st_value;
267 	if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
268 	    (nsym->st_shndx != SHN_ABS))
269 		symval += ADDR(nlmp);
270 	if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) {
271 		/*
272 		 * Record that this new link map is now bound to the caller.
273 		 */
274 		if (bind_one(lmp, nlmp, BND_REFER) == 0)
275 			rtldexit(lml, 1);
276 	}
277 
278 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) {
279 		uint_t	symndx = (((uintptr_t)nsym -
280 		    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
281 		symval = audit_symbind(lmp, nlmp, nsym, symndx, symval,
282 		    &sb_flags);
283 	}
284 
285 	if (!(rtld_flags & RT_FL_NOBIND)) {
286 		addr = rptr->r_offset;
287 		if (!(FLAGS(lmp) & FLG_RT_FIXED))
288 			addr += ADDR(lmp);
289 		if (((lml->lm_tflags | FLAGS1(lmp)) &
290 		    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
291 		    AUDINFO(lmp)->ai_dynplts) {
292 			int	fail = 0;
293 			uint_t	pltndx = reloff / sizeof (Rel);
294 			uint_t	symndx = (((uintptr_t)nsym -
295 			    (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp));
296 
297 			symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp,
298 			    nsym, symndx, pltndx, (caddr_t)symval, sb_flags,
299 			    &fail);
300 			if (fail)
301 				rtldexit(lml, 1);
302 		} else {
303 			/*
304 			 * Write standard PLT entry to jump directly
305 			 * to newly bound function.
306 			 */
307 			*(ulong_t *)addr = symval;
308 		}
309 	}
310 
311 	/*
312 	 * Print binding information and rebuild PLT entry.
313 	 */
314 	DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)),
315 	    (Xword)(reloff / sizeof (Rel)), PLT_T_FULL, nlmp, (Addr)symval,
316 	    nsym->st_value, name, binfo));
317 
318 	/*
319 	 * Complete any processing for newly loaded objects.  Note we don't
320 	 * know exactly where any new objects are loaded (we know the object
321 	 * that supplied the symbol, but others may have been loaded lazily as
322 	 * we searched for the symbol), so sorting starts from the last
323 	 * link-map know on entry to this routine.
324 	 */
325 	if (entry)
326 		load_completion(llmp);
327 
328 	/*
329 	 * Some operations like dldump() or dlopen()'ing a relocatable object
330 	 * result in objects being loaded on rtld's link-map, make sure these
331 	 * objects are initialized also.
332 	 */
333 	if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init)
334 		load_completion(nlmp);
335 
336 	/*
337 	 * If the object we've bound to is in the process of being initialized
338 	 * by another thread, determine whether we should block.
339 	 */
340 	is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL);
341 
342 	/*
343 	 * Make sure the object to which we've bound has had it's .init fired.
344 	 * Cleanup before return to user code.
345 	 */
346 	if (entry) {
347 		is_dep_init(nlmp, lmp);
348 		leave(lml);
349 	}
350 
351 	if (lmflags & LML_FLG_RTLDLM)
352 		dbg_desc->d_class = dbg_class;
353 
354 	return (symval);
355 }
356 
357 
358 /*
359  * When the relocation loop realizes that it's dealing with relative
360  * relocations in a shared object, it breaks into this tighter loop
361  * as an optimization.
362  */
363 ulong_t
364 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
365     ulong_t basebgn, ulong_t etext, ulong_t emap)
366 {
367 	ulong_t roffset = ((Rel *)relbgn)->r_offset;
368 	char rtype;
369 
370 	do {
371 		roffset += basebgn;
372 
373 		/*
374 		 * If this relocation is against an address not mapped in,
375 		 * then break out of the relative relocation loop, falling
376 		 * back on the main relocation loop.
377 		 */
378 		if (roffset < etext || roffset > emap)
379 			break;
380 
381 		/*
382 		 * Perform the actual relocation.
383 		 */
384 		*((ulong_t *)roffset) += basebgn;
385 
386 		relbgn += relsiz;
387 
388 		if (relbgn >= relend)
389 			break;
390 
391 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
392 		roffset = ((Rel *)relbgn)->r_offset;
393 
394 	} while (rtype == R_386_RELATIVE);
395 
396 	return (relbgn);
397 }
398 
399 /*
400  * This is the tightest loop for RELATIVE relocations for those
401  * objects built with the DT_RELACOUNT .dynamic entry.
402  */
403 ulong_t
404 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
405     ulong_t basebgn)
406 {
407 	ulong_t roffset = ((Rel *) relbgn)->r_offset;
408 
409 	for (; relacount; relacount--) {
410 		roffset += basebgn;
411 
412 		/*
413 		 * Perform the actual relocation.
414 		 */
415 		*((ulong_t *)roffset) += basebgn;
416 
417 		relbgn += relsiz;
418 
419 		roffset = ((Rel *)relbgn)->r_offset;
420 
421 	}
422 
423 	return (relbgn);
424 }
425 
426 /*
427  * Read and process the relocations for one link object, we assume all
428  * relocation sections for loadable segments are stored contiguously in
429  * the file.
430  */
431 int
432 elf_reloc(Rt_map *lmp, uint_t plt)
433 {
434 	ulong_t		relbgn, relend, relsiz, basebgn;
435 	ulong_t		pltbgn, pltend, _pltbgn, _pltend;
436 	ulong_t		roffset, rsymndx, psymndx = 0, etext  = ETEXT(lmp);
437 	ulong_t		emap, dsymndx;
438 	uchar_t		rtype;
439 	long		value, pvalue;
440 	Sym		*symref, *psymref, *symdef, *psymdef;
441 	char		*name, *pname;
442 	Rt_map		*_lmp, *plmp;
443 	int		textrel = 0, ret = 1, noplt = 0;
444 	int		relacount = RELACOUNT(lmp), plthint = 0;
445 	Rel		*rel;
446 	uint_t		binfo, pbinfo;
447 	Alist		*bound = 0;
448 
449 	/*
450 	 * Although only necessary for lazy binding, initialize the first
451 	 * global offset entry to go to elf_rtbndr().  dbx(1) seems
452 	 * to find this useful.
453 	 */
454 	if ((plt == 0) && PLTGOT(lmp)) {
455 		if ((ulong_t)PLTGOT(lmp) < etext) {
456 			if (elf_set_prot(lmp, PROT_WRITE) == 0)
457 				return (0);
458 			textrel = 1;
459 		}
460 		elf_plt_init(PLTGOT(lmp), (caddr_t)lmp);
461 	}
462 
463 	/*
464 	 * Initialize the plt start and end addresses.
465 	 */
466 	if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0)
467 		pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp));
468 
469 
470 	relsiz = (ulong_t)(RELENT(lmp));
471 	basebgn = ADDR(lmp);
472 	emap = ADDR(lmp) + MSIZE(lmp);
473 
474 	if (PLTRELSZ(lmp))
475 		plthint = PLTRELSZ(lmp) / relsiz;
476 
477 	/*
478 	 * If we've been called upon to promote an RTLD_LAZY object to an
479 	 * RTLD_NOW then we're only interested in scaning the .plt table.
480 	 * An uninitialized .plt is the case where the associated got entry
481 	 * points back to the plt itself.  Determine the range of the real .plt
482 	 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol.
483 	 */
484 	if (plt) {
485 		Slookup	sl;
486 
487 		relbgn = pltbgn;
488 		relend = pltend;
489 		if (!relbgn || (relbgn == relend))
490 			return (1);
491 
492 		sl.sl_name = MSG_ORIG(MSG_SYM_PLT);
493 		sl.sl_cmap = lmp;
494 		sl.sl_imap = lmp;
495 		sl.sl_hash = elf_hash(MSG_ORIG(MSG_SYM_PLT));
496 		sl.sl_rsymndx = 0;
497 		sl.sl_flags = LKUP_DEFT;
498 
499 		if ((symdef = elf_find_sym(&sl, &_lmp, &binfo)) == 0)
500 			return (1);
501 
502 		_pltbgn = symdef->st_value;
503 		if (!(FLAGS(lmp) & FLG_RT_FIXED) &&
504 		    (symdef->st_shndx != SHN_ABS))
505 			_pltbgn += basebgn;
506 		_pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) *
507 		    M_PLT_ENTSIZE) + M_PLT_RESERVSZ;
508 
509 	} else {
510 		/*
511 		 * The relocation sections appear to the run-time linker as a
512 		 * single table.  Determine the address of the beginning and end
513 		 * of this table.  There are two different interpretations of
514 		 * the ABI at this point:
515 		 *
516 		 *   o	The REL table and its associated RELSZ indicate the
517 		 *	concatenation of *all* relocation sections (this is the
518 		 *	model our link-editor constructs).
519 		 *
520 		 *   o	The REL table and its associated RELSZ indicate the
521 		 *	concatenation of all *but* the .plt relocations.  These
522 		 *	relocations are specified individually by the JMPREL and
523 		 *	PLTRELSZ entries.
524 		 *
525 		 * Determine from our knowledege of the relocation range and
526 		 * .plt range, the range of the total relocation table.  Note
527 		 * that one other ABI assumption seems to be that the .plt
528 		 * relocations always follow any other relocations, the
529 		 * following range checking drops that assumption.
530 		 */
531 		relbgn = (ulong_t)(REL(lmp));
532 		relend = relbgn + (ulong_t)(RELSZ(lmp));
533 		if (pltbgn) {
534 			if (!relbgn || (relbgn > pltbgn))
535 				relbgn = pltbgn;
536 			if (!relbgn || (relend < pltend))
537 				relend = pltend;
538 		}
539 	}
540 	if (!relbgn || (relbgn == relend)) {
541 		DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE));
542 		return (1);
543 	}
544 	DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START));
545 
546 	/*
547 	 * If we're processing a dynamic executable in lazy mode there is no
548 	 * need to scan the .rel.plt table, however if we're processing a shared
549 	 * object in lazy mode the .got addresses associated to each .plt must
550 	 * be relocated to reflect the location of the shared object.
551 	 */
552 	if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) &&
553 	    (FLAGS(lmp) & FLG_RT_FIXED))
554 		noplt = 1;
555 
556 	/*
557 	 * Loop through relocations.
558 	 */
559 	while (relbgn < relend) {
560 		uint_t	sb_flags = 0;
561 
562 		rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
563 
564 		/*
565 		 * If this is a RELATIVE relocation in a shared object (the
566 		 * common case), and if we are not debugging, then jump into a
567 		 * tighter relocation loop (elf_reloc_relative).  Only make the
568 		 * jump if we've been given a hint on the number of relocations.
569 		 */
570 		if ((rtype == R_386_RELATIVE) &&
571 		    ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) {
572 			/*
573 			 * It's possible that the relative relocation block
574 			 * has relocations against the text segment as well
575 			 * as the data segment.  Since our optimized relocation
576 			 * engine does not check which segment the relocation
577 			 * is against - just mprotect it now if it's been
578 			 * marked as containing TEXTREL's.
579 			 */
580 			if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) {
581 				if (elf_set_prot(lmp, PROT_WRITE) == 0) {
582 					ret = 0;
583 					break;
584 				}
585 				textrel = 1;
586 			}
587 
588 			if (relacount) {
589 				relbgn = elf_reloc_relacount(relbgn, relacount,
590 				    relsiz, basebgn);
591 				relacount = 0;
592 			} else {
593 				relbgn = elf_reloc_relative(relbgn, relend,
594 				    relsiz, basebgn, etext, emap);
595 			}
596 			if (relbgn >= relend)
597 				break;
598 			rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info);
599 		}
600 
601 		roffset = ((Rel *)relbgn)->r_offset;
602 
603 		/*
604 		 * If this is a shared object, add the base address to offset.
605 		 */
606 		if (!(FLAGS(lmp) & FLG_RT_FIXED)) {
607 
608 			/*
609 			 * If we're processing lazy bindings, we have to step
610 			 * through the plt entries and add the base address
611 			 * to the corresponding got entry.
612 			 */
613 			if (plthint && (plt == 0) &&
614 			    (rtype == R_386_JMP_SLOT) &&
615 			    ((MODE(lmp) & RTLD_NOW) == 0)) {
616 				relbgn = elf_reloc_relacount(relbgn,
617 				    plthint, relsiz, basebgn);
618 				plthint = 0;
619 				continue;
620 			}
621 			roffset += basebgn;
622 		}
623 
624 		rsymndx = ELF_R_SYM(((Rel *)relbgn)->r_info);
625 		rel = (Rel *)relbgn;
626 		relbgn += relsiz;
627 
628 		/*
629 		 * Optimizations.
630 		 */
631 		if (rtype == R_386_NONE)
632 			continue;
633 		if (noplt && ((ulong_t)rel >= pltbgn) &&
634 		    ((ulong_t)rel < pltend)) {
635 			relbgn = pltend;
636 			continue;
637 		}
638 
639 		/*
640 		 * If we're promoting plts determine if this one has already
641 		 * been written.
642 		 */
643 		if (plt) {
644 			if ((*(ulong_t *)roffset < _pltbgn) ||
645 			    (*(ulong_t *)roffset > _pltend))
646 				continue;
647 		}
648 
649 		/*
650 		 * If this relocation is not against part of the image
651 		 * mapped into memory we skip it.
652 		 */
653 		if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) +
654 		    MSIZE(lmp)))) {
655 			elf_reloc_bad(lmp, (void *)rel, rtype, roffset,
656 			    rsymndx);
657 			continue;
658 		}
659 
660 		binfo = 0;
661 		/*
662 		 * If a symbol index is specified then get the symbol table
663 		 * entry, locate the symbol definition, and determine its
664 		 * address.
665 		 */
666 		if (rsymndx) {
667 			/*
668 			 * Get the local symbol table entry.
669 			 */
670 			symref = (Sym *)((ulong_t)SYMTAB(lmp) +
671 			    (rsymndx * SYMENT(lmp)));
672 
673 			/*
674 			 * If this is a local symbol, just use the base address.
675 			 * (we should have no local relocations in the
676 			 * executable).
677 			 */
678 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
679 				value = basebgn;
680 				name = (char *)0;
681 
682 				/*
683 				 * Special case TLS relocations.
684 				 */
685 				if (rtype == R_386_TLS_DTPMOD32) {
686 					/*
687 					 * Use the TLS modid.
688 					 */
689 					value = TLSMODID(lmp);
690 
691 				} else if (rtype == R_386_TLS_TPOFF) {
692 					if ((value = elf_static_tls(lmp, symref,
693 					    rel, rtype, 0, roffset, 0)) == 0) {
694 						ret = 0;
695 						break;
696 					}
697 				}
698 			} else {
699 				/*
700 				 * If the symbol index is equal to the previous
701 				 * symbol index relocation we processed then
702 				 * reuse the previous values. (Note that there
703 				 * have been cases where a relocation exists
704 				 * against a copy relocation symbol, our ld(1)
705 				 * should optimize this away, but make sure we
706 				 * don't use the same symbol information should
707 				 * this case exist).
708 				 */
709 				if ((rsymndx == psymndx) &&
710 				    (rtype != R_386_COPY)) {
711 					/* LINTED */
712 					if (psymdef == 0) {
713 						DBG_CALL(Dbg_bind_weak(lmp,
714 						    (Addr)roffset, (Addr)
715 						    (roffset - basebgn), name));
716 						continue;
717 					}
718 					/* LINTED */
719 					value = pvalue;
720 					/* LINTED */
721 					name = pname;
722 					/* LINTED */
723 					symdef = psymdef;
724 					/* LINTED */
725 					symref = psymref;
726 					/* LINTED */
727 					_lmp = plmp;
728 					/* LINTED */
729 					binfo = pbinfo;
730 
731 					if ((LIST(_lmp)->lm_tflags |
732 					    FLAGS1(_lmp)) &
733 					    LML_TFLG_AUD_SYMBIND) {
734 						value = audit_symbind(lmp, _lmp,
735 						    /* LINTED */
736 						    symdef, dsymndx, value,
737 						    &sb_flags);
738 					}
739 				} else {
740 					Slookup		sl;
741 					uchar_t		bind;
742 
743 					/*
744 					 * Lookup the symbol definition.
745 					 */
746 					name = (char *)(STRTAB(lmp) +
747 					    symref->st_name);
748 
749 					sl.sl_name = name;
750 					sl.sl_cmap = lmp;
751 					sl.sl_imap = 0;
752 					sl.sl_hash = 0;
753 					sl.sl_rsymndx = rsymndx;
754 
755 					if (rtype == R_386_COPY)
756 						sl.sl_flags = LKUP_COPY;
757 					else
758 						sl.sl_flags = LKUP_DEFT;
759 
760 					sl.sl_flags |= LKUP_ALLCNTLIST;
761 
762 					if (rtype != R_386_JMP_SLOT)
763 						sl.sl_flags |= LKUP_SPEC;
764 
765 					/*
766 					 * Under ldd -w, any unresolved weak
767 					 * references are diagnosed.  Set the
768 					 * symbol binding as global to trigger
769 					 * a relocation error if the symbol can
770 					 * not be found.
771 					 */
772 					if (LIST(lmp)->lm_flags &
773 					    LML_FLG_TRC_NOUNRESWEAK) {
774 						bind = STB_GLOBAL;
775 					} else if ((bind =
776 					    ELF_ST_BIND(symref->st_info)) ==
777 					    STB_WEAK) {
778 						sl.sl_flags |= LKUP_WEAK;
779 					}
780 
781 					symdef = lookup_sym(&sl, &_lmp, &binfo);
782 
783 					/*
784 					 * If the symbol is not found and the
785 					 * reference was not to a weak symbol,
786 					 * report an error.  Weak references
787 					 * may be unresolved.
788 					 * chkmsg: MSG_INTL(MSG_LDD_SYM_NFOUND)
789 					 */
790 					/* BEGIN CSTYLED */
791 					if (symdef == 0) {
792 					    Lm_list	*lml = LIST(lmp);
793 
794 					    if (bind != STB_WEAK) {
795 						if (lml->lm_flags &
796 						    LML_FLG_IGNRELERR) {
797 						    continue;
798 						} else if (lml->lm_flags &
799 						    LML_FLG_TRC_WARN) {
800 						    (void) printf(MSG_INTL(
801 							MSG_LDD_SYM_NFOUND),
802 							demangle(name),
803 							NAME(lmp));
804 						    continue;
805 						} else {
806 						    DBG_CALL(Dbg_reloc_in(lml,
807 							ELF_DBG_RTLD, M_MACH,
808 							M_REL_SHT_TYPE, rel,
809 							NULL, name));
810 						    eprintf(lml, ERR_FATAL,
811 							MSG_INTL(MSG_REL_NOSYM),
812 							NAME(lmp),
813 							demangle(name));
814 						    ret = 0;
815 						    break;
816 						}
817 					    } else {
818 						psymndx = rsymndx;
819 						psymdef = 0;
820 
821 						DBG_CALL(Dbg_bind_weak(lmp,
822 						    (Addr)roffset, (Addr)
823 						    (roffset - basebgn), name));
824 						continue;
825 					    }
826 					}
827 					/* END CSTYLED */
828 
829 					/*
830 					 * If symbol was found in an object
831 					 * other than the referencing object
832 					 * then record the binding.
833 					 */
834 					if ((lmp != _lmp) && ((FLAGS1(_lmp) &
835 					    FL1_RT_NOINIFIN) == 0)) {
836 						if (alist_test(&bound, _lmp,
837 						    sizeof (Rt_map *),
838 						    AL_CNT_RELBIND) == 0) {
839 							ret = 0;
840 							break;
841 						}
842 					}
843 
844 					/*
845 					 * Calculate the location of definition;
846 					 * symbol value plus base address of
847 					 * containing shared object.
848 					 */
849 					if (IS_SIZE(rtype))
850 						value = symdef->st_size;
851 					else
852 						value = symdef->st_value;
853 
854 					if (!(FLAGS(_lmp) & FLG_RT_FIXED) &&
855 					    !(IS_SIZE(rtype)) &&
856 					    (symdef->st_shndx != SHN_ABS) &&
857 					    (ELF_ST_TYPE(symdef->st_info) !=
858 					    STT_TLS))
859 						value += ADDR(_lmp);
860 
861 					/*
862 					 * Retain this symbol index and the
863 					 * value in case it can be used for the
864 					 * subsequent relocations.
865 					 */
866 					if (rtype != R_386_COPY) {
867 						psymndx = rsymndx;
868 						pvalue = value;
869 						pname = name;
870 						psymdef = symdef;
871 						psymref = symref;
872 						plmp = _lmp;
873 						pbinfo = binfo;
874 					}
875 					if ((LIST(_lmp)->lm_tflags |
876 					    FLAGS1(_lmp)) &
877 					    LML_TFLG_AUD_SYMBIND) {
878 						dsymndx = (((uintptr_t)symdef -
879 						    (uintptr_t)SYMTAB(_lmp)) /
880 						    SYMENT(_lmp));
881 						value = audit_symbind(lmp, _lmp,
882 						    symdef, dsymndx, value,
883 						    &sb_flags);
884 					}
885 				}
886 
887 				/*
888 				 * If relocation is PC-relative, subtract
889 				 * offset address.
890 				 */
891 				if (IS_PC_RELATIVE(rtype))
892 					value -= roffset;
893 
894 				/*
895 				 * Special case TLS relocations.
896 				 */
897 				if (rtype == R_386_TLS_DTPMOD32) {
898 					/*
899 					 * Relocation value is the TLS modid.
900 					 */
901 					value = TLSMODID(_lmp);
902 
903 				} else if (rtype == R_386_TLS_TPOFF) {
904 					if ((value = elf_static_tls(_lmp,
905 					    symdef, rel, rtype, name, roffset,
906 					    value)) == 0) {
907 						ret = 0;
908 						break;
909 					}
910 				}
911 			}
912 		} else {
913 			/*
914 			 * Special cases.
915 			 */
916 			if (rtype == R_386_TLS_DTPMOD32) {
917 				/*
918 				 * TLS relocation value is the TLS modid.
919 				 */
920 				value = TLSMODID(lmp);
921 			} else
922 				value = basebgn;
923 			name = (char *)0;
924 		}
925 
926 		DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH,
927 		    M_REL_SHT_TYPE, rel, NULL, name));
928 
929 		/*
930 		 * If this object has relocations in the text segment, turn
931 		 * off the write protect.
932 		 */
933 		if ((roffset < etext) && (textrel == 0)) {
934 			if (elf_set_prot(lmp, PROT_WRITE) == 0) {
935 				ret = 0;
936 				break;
937 			}
938 			textrel = 1;
939 		}
940 
941 		/*
942 		 * Call relocation routine to perform required relocation.
943 		 */
944 		switch (rtype) {
945 		case R_386_COPY:
946 			if (elf_copy_reloc(name, symref, lmp, (void *)roffset,
947 			    symdef, _lmp, (const void *)value) == 0)
948 				ret = 0;
949 			break;
950 		case R_386_JMP_SLOT:
951 			if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) &
952 			    (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
953 			    AUDINFO(lmp)->ai_dynplts) {
954 				int	fail = 0;
955 				int	pltndx = (((ulong_t)rel -
956 				    (uintptr_t)JMPREL(lmp)) / relsiz);
957 				int	symndx = (((uintptr_t)symdef -
958 				    (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp));
959 
960 				(void) elf_plt_trace_write(roffset, lmp, _lmp,
961 				    symdef, symndx, pltndx, (caddr_t)value,
962 				    sb_flags, &fail);
963 				if (fail)
964 					ret = 0;
965 			} else {
966 				/*
967 				 * Write standard PLT entry to jump directly
968 				 * to newly bound function.
969 				 */
970 				DBG_CALL(Dbg_reloc_apply_val(LIST(lmp),
971 				    ELF_DBG_RTLD, (Xword)roffset,
972 				    (Xword)value));
973 				*(ulong_t *)roffset = value;
974 			}
975 			break;
976 		default:
977 			/*
978 			 * Write the relocation out.
979 			 */
980 			if (do_reloc(rtype, (uchar_t *)roffset, (Word *)&value,
981 			    name, NAME(lmp), LIST(lmp)) == 0)
982 				ret = 0;
983 
984 			DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD,
985 			    (Xword)roffset, (Xword)value));
986 		}
987 
988 		if ((ret == 0) &&
989 		    ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0))
990 			break;
991 
992 		if (binfo) {
993 			DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset,
994 			    (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL,
995 			    _lmp, (Addr)value, symdef->st_value, name, binfo));
996 		}
997 	}
998 
999 	return (relocate_finish(lmp, bound, textrel, ret));
1000 }
1001 
1002 /*
1003  * Initialize the first few got entries so that function calls go to
1004  * elf_rtbndr:
1005  *
1006  *	GOT[GOT_XLINKMAP] =	the address of the link map
1007  *	GOT[GOT_XRTLD] =	the address of rtbinder
1008  */
1009 void
1010 elf_plt_init(void *got, caddr_t l)
1011 {
1012 	uint_t		*_got;
1013 	/* LINTED */
1014 	Rt_map		*lmp = (Rt_map *)l;
1015 
1016 	_got = (uint_t *)got + M_GOT_XLINKMAP;
1017 	*_got = (uint_t)lmp;
1018 	_got = (uint_t *)got + M_GOT_XRTLD;
1019 	*_got = (uint_t)elf_rtbndr;
1020 }
1021 
1022 /*
1023  * For SVR4 Intel compatability.  USL uses /usr/lib/libc.so.1 as the run-time
1024  * linker, so the interpreter's address will differ from /usr/lib/ld.so.1.
1025  * Further, USL has special _iob[] and _ctype[] processing that makes up for the
1026  * fact that these arrays do not have associated copy relocations.  So we try
1027  * and make up for that here.  Any relocations found will be added to the global
1028  * copy relocation list and will be processed in setup().
1029  */
1030 static int
1031 _elf_copy_reloc(const char *name, Rt_map *rlmp, Rt_map *dlmp)
1032 {
1033 	Sym		*symref, *symdef;
1034 	caddr_t 	ref, def;
1035 	Rt_map		*_lmp;
1036 	Rel		rel;
1037 	Slookup		sl;
1038 	uint_t		binfo;
1039 
1040 	/*
1041 	 * Determine if the special symbol exists as a reference in the dynamic
1042 	 * executable, and that an associated definition exists in libc.so.1.
1043 	 */
1044 	sl.sl_name = name;
1045 	sl.sl_cmap = rlmp;
1046 	sl.sl_imap = rlmp;
1047 	sl.sl_hash = 0;
1048 	sl.sl_rsymndx = 0;
1049 	sl.sl_flags = LKUP_FIRST;
1050 
1051 	if ((symref = lookup_sym(&sl, &_lmp, &binfo)) == 0)
1052 		return (1);
1053 
1054 	sl.sl_imap = dlmp;
1055 	sl.sl_flags = LKUP_DEFT;
1056 
1057 	if ((symdef = lookup_sym(&sl, &_lmp, &binfo)) == 0)
1058 		return (1);
1059 	if (strcmp(NAME(_lmp), MSG_ORIG(MSG_PTH_LIBC)))
1060 		return (1);
1061 
1062 	/*
1063 	 * Determine the reference and definition addresses.
1064 	 */
1065 	ref = (void *)(symref->st_value);
1066 	if (!(FLAGS(rlmp) & FLG_RT_FIXED))
1067 		ref += ADDR(rlmp);
1068 	def = (void *)(symdef->st_value);
1069 	if (!(FLAGS(_lmp) & FLG_RT_FIXED))
1070 		def += ADDR(_lmp);
1071 
1072 	/*
1073 	 * Set up a relocation entry for debugging and call the generic copy
1074 	 * relocation function to provide symbol size error checking and to
1075 	 * record the copy relocation that must be performed.
1076 	 */
1077 	rel.r_offset = (Addr)ref;
1078 	rel.r_info = (Word)R_386_COPY;
1079 	DBG_CALL(Dbg_reloc_in(LIST(rlmp), ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE,
1080 	    &rel, NULL, name));
1081 
1082 	return (elf_copy_reloc((char *)name, symref, rlmp, (void *)ref, symdef,
1083 	    _lmp, (void *)def));
1084 }
1085 
1086 int
1087 elf_copy_gen(Rt_map *lmp)
1088 {
1089 	if (interp && ((ulong_t)interp->i_faddr !=
1090 	    r_debug.rtd_rdebug.r_ldbase) &&
1091 	    !(strcmp(interp->i_name, MSG_ORIG(MSG_PTH_LIBC)))) {
1092 
1093 		DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, 0,
1094 		    DBG_REL_START));
1095 
1096 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_CTYPE), lmp,
1097 		    (Rt_map *)NEXT(lmp)) == 0)
1098 			return (0);
1099 		if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_IOB), lmp,
1100 		    (Rt_map *)NEXT(lmp)) == 0)
1101 			return (0);
1102 	}
1103 	return (1);
1104 }
1105 
1106 /*
1107  * Plt writing interface to allow debugging initialization to be generic.
1108  */
1109 Pltbindtype
1110 /* ARGSUSED1 */
1111 elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval,
1112 	Xword pltndx)
1113 {
1114 	Rel		*rel = (Rel*)rptr;
1115 	uintptr_t	pltaddr;
1116 
1117 	pltaddr = addr + rel->r_offset;
1118 	*(ulong_t *)pltaddr = (ulong_t)symval;
1119 	DBG_CALL(pltcntfull++);
1120 	return (PLT_T_FULL);
1121 }
1122 
1123 /*
1124  * Provide a machine specific interface to the conversion routine.  By calling
1125  * the machine specific version, rather than the generic version, we insure that
1126  * the data tables/strings for all known machine versions aren't dragged into
1127  * ld.so.1.
1128  */
1129 const char *
1130 _conv_reloc_type(uint_t rel)
1131 {
1132 	static Conv_inv_buf_t inv_buf;
1133 
1134 	return (conv_reloc_386_type(rel, 0, &inv_buf));
1135 }
1136