xref: /onnv-gate/usr/src/cmd/sgs/librtld/sparc/_relocate.c (revision 11053:f33a1c7f3155)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
23*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
241618Srie  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include	<string.h>
280Sstevel@tonic-gate #include	"machdep.h"
290Sstevel@tonic-gate #include	"reloc.h"
300Sstevel@tonic-gate #include	"_librtld.h"
310Sstevel@tonic-gate #include	"_elf.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Undo relocations that have been applied to a memory image.  Basically this
350Sstevel@tonic-gate  * involves copying the original files relocation offset into the new image
360Sstevel@tonic-gate  * being created.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate /* ARGSUSED3 */
390Sstevel@tonic-gate void
undo_reloc(void * vrel,uchar_t * oaddr,uchar_t * iaddr,Reloc * reloc)401618Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	Rela		*rel = vrel;
430Sstevel@tonic-gate 	const Rel_entry	*rep;
446206Sab196087 	Xword		rtype = ELF_R_TYPE(rel->r_info, M_MACH);
451618Srie 	ulong_t		*_oaddr;
461618Srie 	ulong_t		*_iaddr;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	switch (rtype) {
490Sstevel@tonic-gate 	case R_SPARC_NONE:
500Sstevel@tonic-gate 		break;
510Sstevel@tonic-gate 	case R_SPARC_COPY:
520Sstevel@tonic-gate 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
530Sstevel@tonic-gate 		break;
540Sstevel@tonic-gate 	case R_SPARC_JMP_SLOT:
550Sstevel@tonic-gate 		/* LINTED */
560Sstevel@tonic-gate 		_oaddr = (unsigned long *)oaddr;
570Sstevel@tonic-gate 		/* LINTED */
580Sstevel@tonic-gate 		_iaddr = (unsigned long *)iaddr;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 		if (_iaddr) {
610Sstevel@tonic-gate 			*_oaddr++ = *_iaddr++;
620Sstevel@tonic-gate 			*_oaddr++ = *_iaddr++;
630Sstevel@tonic-gate 			*_oaddr = *_iaddr;
640Sstevel@tonic-gate 		} else {
650Sstevel@tonic-gate 			*_oaddr++ = 0;
660Sstevel@tonic-gate 			*_oaddr++ = 0;
670Sstevel@tonic-gate 			*_oaddr = 0;
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 		break;
700Sstevel@tonic-gate 	default:
710Sstevel@tonic-gate 		rep = &reloc_table[rtype];
721618Srie 		if (iaddr)
730Sstevel@tonic-gate 			(void) memcpy(oaddr, iaddr, rep->re_fsize);
740Sstevel@tonic-gate 		else
750Sstevel@tonic-gate 			(void) memset(oaddr, 0, rep->re_fsize);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Copy a relocation record and increment its value.  The record must reflect
810Sstevel@tonic-gate  * the new address to which this image is fixed.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate /* ARGSUSED3 */
840Sstevel@tonic-gate void
inc_reloc(void * vnrel,void * vorel,Reloc * reloc,uchar_t * oaddr,uchar_t * iaddr)851618Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
861618Srie     uchar_t *iaddr)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	Rela	*nrel = vnrel;
890Sstevel@tonic-gate 	Rela	*orel = vorel;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	*nrel = *orel;
920Sstevel@tonic-gate 	nrel->r_offset += reloc->r_value;
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Clear a relocation record.  The relocation has been applied to the image and
970Sstevel@tonic-gate  * thus the relocation must not occur again.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate void
clear_reloc(void * vrel)1000Sstevel@tonic-gate clear_reloc(void *vrel)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	Rela	*rel = vrel;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	rel->r_offset = 0;
1050Sstevel@tonic-gate 	rel->r_info = ELF_R_INFO(0, R_SPARC_NONE);
1060Sstevel@tonic-gate 	rel->r_addend = 0;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Apply a relocation to an image being built from an input file.  Use the
1110Sstevel@tonic-gate  * runtime linkers routines to do the necessary magic.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate void
apply_reloc(void * vrel,Reloc * reloc,const char * name,uchar_t * oaddr,Rt_map * lmp)1141618Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
1151618Srie     Rt_map *lmp)
1160Sstevel@tonic-gate {
1171618Srie 	Rela	*rel = vrel;
1186206Sab196087 	Xword	type = ELF_R_TYPE(rel->r_info, M_MACH);
1191618Srie 	Xword	value = reloc->r_value + rel->r_addend;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (type == R_SPARC_JMP_SLOT) {
1220Sstevel@tonic-gate 		uintptr_t	addr, vaddr;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_FIXED)
1250Sstevel@tonic-gate 			vaddr = 0;
1260Sstevel@tonic-gate 		else
1270Sstevel@tonic-gate 			vaddr = ADDR(lmp);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		addr = (uintptr_t)oaddr - rel->r_offset;
1300Sstevel@tonic-gate 		/* LINTED */
131*11053SSurya.Prakki@Sun.COM 		(void) elf_plt_write((uintptr_t)addr, vaddr, rel,
1320Sstevel@tonic-gate 		    (uintptr_t)value, reloc->r_pltndx);
1331618Srie 
1340Sstevel@tonic-gate 	} else if (type == R_SPARC_COPY) {
1350Sstevel@tonic-gate 		(void) memcpy((void *)oaddr, (void *)value,
1360Sstevel@tonic-gate 		    (size_t)reloc->r_size);
1370Sstevel@tonic-gate 	} else {
1381618Srie 		if (IS_EXTOFFSET(type))
1391618Srie 			value += ELF_R_TYPE_DATA(rel->r_info);
1405189Sab196087 		(void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
1411618Srie 		    LIST(lmp));
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate }
144