xref: /onnv-gate/usr/src/cmd/sgs/librtld/amd64/_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.
240Sstevel@tonic-gate  * 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  */
381618Srie /* 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 {
421618Srie 	Rela		*rel = vrel;
436206Sab196087 	Xword		rtype = ELF_R_TYPE(rel->r_info, M_MACH);
440Sstevel@tonic-gate 
451618Srie 	switch (rtype) {
461618Srie 	case R_AMD64_NONE:
470Sstevel@tonic-gate 		break;
481618Srie 	case R_AMD64_COPY:
490Sstevel@tonic-gate 		(void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
500Sstevel@tonic-gate 		break;
511618Srie 	case R_AMD64_JUMP_SLOT:
527082Sab196087 		{
537082Sab196087 			/* LINTED */
547082Sab196087 			ulong_t *_oaddr = (ulong_t *)oaddr;
557082Sab196087 			/* LINTED */
567082Sab196087 			ulong_t *_iaddr = (ulong_t *)iaddr;
570Sstevel@tonic-gate 
587082Sab196087 			if (_iaddr)
597082Sab196087 				*_oaddr = *_iaddr + reloc->r_value;
607082Sab196087 			else
617082Sab196087 				*_oaddr = reloc->r_value;
621618Srie 		}
630Sstevel@tonic-gate 		break;
640Sstevel@tonic-gate 	default:
657082Sab196087 		{
667082Sab196087 			size_t re_fsize = reloc_table[rtype].re_fsize;
677082Sab196087 
687082Sab196087 			if (iaddr)
697082Sab196087 				(void) memcpy(oaddr, iaddr, re_fsize);
707082Sab196087 			else
717082Sab196087 				(void) memset(oaddr, 0, re_fsize);
727082Sab196087 		}
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Copy a relocation record and increment its value.  The record must reflect
787082Sab196087  * the new address to which this image is fixed. Note that .got entries
797082Sab196087  * associated with .plt's must be fixed to the new base address.
800Sstevel@tonic-gate  */
811618Srie /* ARGSUSED3 */
820Sstevel@tonic-gate void
inc_reloc(void * vnrel,void * vorel,Reloc * reloc,uchar_t * oaddr,uchar_t * iaddr)831618Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
841618Srie     uchar_t *iaddr)
850Sstevel@tonic-gate {
861618Srie 	Rela	*nrel = vnrel;
871618Srie 	Rela	*orel = vorel;
880Sstevel@tonic-gate 
897082Sab196087 	if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_AMD64_JUMP_SLOT) {
907082Sab196087 		/* LINTED */
917082Sab196087 		ulong_t	*_oaddr = (ulong_t *)oaddr;
927082Sab196087 		/* LINTED */
937082Sab196087 		ulong_t	*_iaddr = (ulong_t *)iaddr;
947082Sab196087 
957082Sab196087 		if (_iaddr)
967082Sab196087 			*_oaddr = *_iaddr + reloc->r_value;
977082Sab196087 		else
987082Sab196087 			*_oaddr = reloc->r_value;
997082Sab196087 	}
1007082Sab196087 
1010Sstevel@tonic-gate 	*nrel = *orel;
1020Sstevel@tonic-gate 	nrel->r_offset += reloc->r_value;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Clear a relocation record.  The relocation has been applied to the image and
1070Sstevel@tonic-gate  * thus the relocation must not occur again.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate void
clear_reloc(void * vrel)1101618Srie clear_reloc(void *vrel)
1110Sstevel@tonic-gate {
1121618Srie 	Rela	*rel = vrel;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	rel->r_offset = 0;
1151618Srie 	rel->r_info = ELF_R_INFO(0, R_AMD64_NONE);
1161618Srie 	rel->r_addend = 0;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Apply a relocation to an image being built from an input file.  Use the
1210Sstevel@tonic-gate  * runtime linkers routines to do the necessary magic.
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate void
apply_reloc(void * vrel,Reloc * reloc,const char * name,uchar_t * oaddr,Rt_map * lmp)1241618Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
1251618Srie     Rt_map *lmp)
1260Sstevel@tonic-gate {
1271618Srie 	Rela	*rel = vrel;
1286206Sab196087 	Xword	type = ELF_R_TYPE(rel->r_info, M_MACH);
1291618Srie 	Xword	value = reloc->r_value + rel->r_addend;
1300Sstevel@tonic-gate 
1311618Srie 	if (type == R_AMD64_JUMP_SLOT) {
1320Sstevel@tonic-gate 		uintptr_t	addr, vaddr;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_FIXED)
1350Sstevel@tonic-gate 			vaddr = 0;
1360Sstevel@tonic-gate 		else
1370Sstevel@tonic-gate 			vaddr = ADDR(lmp);
1381618Srie 
1390Sstevel@tonic-gate 		addr = (uintptr_t)oaddr - rel->r_offset;
1400Sstevel@tonic-gate 		/* LINTED */
141*11053SSurya.Prakki@Sun.COM 		(void) elf_plt_write((uintptr_t)addr, vaddr, rel,
1420Sstevel@tonic-gate 		    (uintptr_t)value, reloc->r_pltndx);
1431618Srie 
1441618Srie 	} else if (type == R_AMD64_COPY) {
1450Sstevel@tonic-gate 		(void) memcpy((void *)oaddr, (void *)value,
1460Sstevel@tonic-gate 		    (size_t)reloc->r_size);
1470Sstevel@tonic-gate 	} else {
1485189Sab196087 		(void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
1491618Srie 		    LIST(lmp));
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate }
152