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 void
undo_reloc(void * vrel,uchar_t * oaddr,uchar_t * iaddr,Reloc * reloc)391618Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
400Sstevel@tonic-gate {
411618Srie Rel *rel = vrel;
420Sstevel@tonic-gate /* LINTED */
431618Srie ulong_t *_oaddr = (ulong_t *)oaddr;
440Sstevel@tonic-gate /* LINTED */
451618Srie ulong_t *_iaddr = (ulong_t *)iaddr;
460Sstevel@tonic-gate
476206Sab196087 switch (ELF_R_TYPE(rel->r_info, M_MACH)) {
480Sstevel@tonic-gate case R_386_NONE:
490Sstevel@tonic-gate break;
500Sstevel@tonic-gate
510Sstevel@tonic-gate case R_386_COPY:
520Sstevel@tonic-gate (void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
530Sstevel@tonic-gate break;
540Sstevel@tonic-gate
550Sstevel@tonic-gate case R_386_JMP_SLOT:
560Sstevel@tonic-gate if (_iaddr)
570Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value;
580Sstevel@tonic-gate else
590Sstevel@tonic-gate *_oaddr = reloc->r_value;
600Sstevel@tonic-gate break;
610Sstevel@tonic-gate
620Sstevel@tonic-gate default:
630Sstevel@tonic-gate if (_iaddr)
640Sstevel@tonic-gate *_oaddr = *_iaddr;
650Sstevel@tonic-gate else
660Sstevel@tonic-gate *_oaddr = 0;
670Sstevel@tonic-gate break;
680Sstevel@tonic-gate }
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Copy a relocation record and increment its value. The record must reflect
730Sstevel@tonic-gate * the new address to which this image is fixed. Note that .got entries
740Sstevel@tonic-gate * associated with .plt's must be fixed to the new base address.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate void
inc_reloc(void * vnrel,void * vorel,Reloc * reloc,uchar_t * oaddr,uchar_t * iaddr)771618Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
781618Srie uchar_t *iaddr)
790Sstevel@tonic-gate {
801618Srie Rel *nrel = vnrel;
811618Srie Rel *orel = vorel;
820Sstevel@tonic-gate /* LINTED */
831618Srie ulong_t *_oaddr = (ulong_t *)oaddr;
840Sstevel@tonic-gate /* LINTED */
851618Srie ulong_t *_iaddr = (ulong_t *)iaddr;
860Sstevel@tonic-gate
876206Sab196087 if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_386_JMP_SLOT) {
880Sstevel@tonic-gate if (_iaddr)
890Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value;
900Sstevel@tonic-gate else
910Sstevel@tonic-gate *_oaddr = reloc->r_value;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate *nrel = *orel;
950Sstevel@tonic-gate nrel->r_offset += reloc->r_value;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * Clear a relocation record. The relocation has been applied to the image and
1000Sstevel@tonic-gate * thus the relocation must not occur again.
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate void
clear_reloc(void * vrel)1031618Srie clear_reloc(void *vrel)
1040Sstevel@tonic-gate {
1051618Srie Rel *rel = vrel;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate rel->r_offset = 0;
1080Sstevel@tonic-gate rel->r_info = ELF_R_INFO(0, R_386_NONE);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * Apply a relocation to an image being built from an input file. Use the
1130Sstevel@tonic-gate * runtime linkers routines to do the necessary magic.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate void
apply_reloc(void * vrel,Reloc * reloc,const char * name,uchar_t * oaddr,Rt_map * lmp)1161618Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
1171618Srie Rt_map *lmp)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate Rel *rel = vrel;
1206206Sab196087 Xword type = ELF_R_TYPE(rel->r_info, M_MACH);
1210Sstevel@tonic-gate Word value = reloc->r_value;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (type == R_386_JMP_SLOT) {
1240Sstevel@tonic-gate uintptr_t addr, vaddr;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (FLAGS(lmp) & FLG_RT_FIXED)
1270Sstevel@tonic-gate vaddr = 0;
1280Sstevel@tonic-gate else
1290Sstevel@tonic-gate vaddr = ADDR(lmp);
1300Sstevel@tonic-gate addr = (uintptr_t)oaddr - rel->r_offset;
1310Sstevel@tonic-gate /* LINTED */
132*11053SSurya.Prakki@Sun.COM (void) elf_plt_write((uintptr_t)addr, vaddr, rel,
1330Sstevel@tonic-gate (uintptr_t)value, reloc->r_pltndx);
1341618Srie
1350Sstevel@tonic-gate } else if (type == R_386_COPY) {
1360Sstevel@tonic-gate (void) memcpy((void *)oaddr, (void *)value,
1370Sstevel@tonic-gate (size_t)reloc->r_size);
1380Sstevel@tonic-gate } else {
1395189Sab196087 (void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
1401618Srie LIST(lmp));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate }
143