17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
215aefb655Srie
227c478bd9Sstevel@tonic-gate /*
23*80148899SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
245aefb655Srie * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include "machdep.h"
297c478bd9Sstevel@tonic-gate #include "reloc.h"
307c478bd9Sstevel@tonic-gate #include "_librtld.h"
317c478bd9Sstevel@tonic-gate #include "_elf.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * Undo relocations that have been applied to a memory image. Basically this
357c478bd9Sstevel@tonic-gate * involves copying the original files relocation offset into the new image
367c478bd9Sstevel@tonic-gate * being created.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate void
undo_reloc(void * vrel,uchar_t * oaddr,uchar_t * iaddr,Reloc * reloc)395aefb655Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate Rel *rel = vrel;
427c478bd9Sstevel@tonic-gate /* LINTED */
435aefb655Srie ulong_t *_oaddr = (ulong_t *)oaddr;
447c478bd9Sstevel@tonic-gate /* LINTED */
455aefb655Srie ulong_t *_iaddr = (ulong_t *)iaddr;
467c478bd9Sstevel@tonic-gate
47ba2be530Sab196087 switch (ELF_R_TYPE(rel->r_info, M_MACH)) {
487c478bd9Sstevel@tonic-gate case R_386_NONE:
497c478bd9Sstevel@tonic-gate break;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate case R_386_COPY:
527c478bd9Sstevel@tonic-gate (void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
537c478bd9Sstevel@tonic-gate break;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate case R_386_JMP_SLOT:
567c478bd9Sstevel@tonic-gate if (_iaddr)
577c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value;
587c478bd9Sstevel@tonic-gate else
597c478bd9Sstevel@tonic-gate *_oaddr = reloc->r_value;
607c478bd9Sstevel@tonic-gate break;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate default:
637c478bd9Sstevel@tonic-gate if (_iaddr)
647c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr;
657c478bd9Sstevel@tonic-gate else
667c478bd9Sstevel@tonic-gate *_oaddr = 0;
677c478bd9Sstevel@tonic-gate break;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * Copy a relocation record and increment its value. The record must reflect
737c478bd9Sstevel@tonic-gate * the new address to which this image is fixed. Note that .got entries
747c478bd9Sstevel@tonic-gate * associated with .plt's must be fixed to the new base address.
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate void
inc_reloc(void * vnrel,void * vorel,Reloc * reloc,uchar_t * oaddr,uchar_t * iaddr)775aefb655Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
785aefb655Srie uchar_t *iaddr)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate Rel *nrel = vnrel;
817c478bd9Sstevel@tonic-gate Rel *orel = vorel;
827c478bd9Sstevel@tonic-gate /* LINTED */
835aefb655Srie ulong_t *_oaddr = (ulong_t *)oaddr;
847c478bd9Sstevel@tonic-gate /* LINTED */
855aefb655Srie ulong_t *_iaddr = (ulong_t *)iaddr;
867c478bd9Sstevel@tonic-gate
87ba2be530Sab196087 if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_386_JMP_SLOT) {
887c478bd9Sstevel@tonic-gate if (_iaddr)
897c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value;
907c478bd9Sstevel@tonic-gate else
917c478bd9Sstevel@tonic-gate *_oaddr = reloc->r_value;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate *nrel = *orel;
957c478bd9Sstevel@tonic-gate nrel->r_offset += reloc->r_value;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Clear a relocation record. The relocation has been applied to the image and
1007c478bd9Sstevel@tonic-gate * thus the relocation must not occur again.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate void
clear_reloc(void * vrel)1037c478bd9Sstevel@tonic-gate clear_reloc(void *vrel)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate Rel *rel = vrel;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate rel->r_offset = 0;
1087c478bd9Sstevel@tonic-gate rel->r_info = ELF_R_INFO(0, R_386_NONE);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Apply a relocation to an image being built from an input file. Use the
1137c478bd9Sstevel@tonic-gate * runtime linkers routines to do the necessary magic.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate void
apply_reloc(void * vrel,Reloc * reloc,const char * name,uchar_t * oaddr,Rt_map * lmp)1165aefb655Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
1175aefb655Srie Rt_map *lmp)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate Rel *rel = vrel;
120ba2be530Sab196087 Xword type = ELF_R_TYPE(rel->r_info, M_MACH);
1217c478bd9Sstevel@tonic-gate Word value = reloc->r_value;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if (type == R_386_JMP_SLOT) {
1247c478bd9Sstevel@tonic-gate uintptr_t addr, vaddr;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate if (FLAGS(lmp) & FLG_RT_FIXED)
1277c478bd9Sstevel@tonic-gate vaddr = 0;
1287c478bd9Sstevel@tonic-gate else
1297c478bd9Sstevel@tonic-gate vaddr = ADDR(lmp);
1307c478bd9Sstevel@tonic-gate addr = (uintptr_t)oaddr - rel->r_offset;
1317c478bd9Sstevel@tonic-gate /* LINTED */
132*80148899SSurya Prakki (void) elf_plt_write((uintptr_t)addr, vaddr, rel,
1337c478bd9Sstevel@tonic-gate (uintptr_t)value, reloc->r_pltndx);
1345aefb655Srie
1357c478bd9Sstevel@tonic-gate } else if (type == R_386_COPY) {
1367c478bd9Sstevel@tonic-gate (void) memcpy((void *)oaddr, (void *)value,
1377c478bd9Sstevel@tonic-gate (size_t)reloc->r_size);
1387c478bd9Sstevel@tonic-gate } else {
139f3324781Sab196087 (void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
1405aefb655Srie LIST(lmp));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate }
143