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 /* ARGSUSED3 */
397c478bd9Sstevel@tonic-gate void
undo_reloc(void * vrel,uchar_t * oaddr,uchar_t * iaddr,Reloc * reloc)405aefb655Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate Rela *rel = vrel;
437c478bd9Sstevel@tonic-gate const Rel_entry *rep;
44ba2be530Sab196087 Xword rtype = ELF_R_TYPE(rel->r_info, M_MACH);
455aefb655Srie ulong_t *_oaddr;
465aefb655Srie ulong_t *_iaddr;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate switch (rtype) {
497c478bd9Sstevel@tonic-gate case R_SPARC_NONE:
507c478bd9Sstevel@tonic-gate break;
517c478bd9Sstevel@tonic-gate case R_SPARC_COPY:
527c478bd9Sstevel@tonic-gate (void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
537c478bd9Sstevel@tonic-gate break;
547c478bd9Sstevel@tonic-gate case R_SPARC_JMP_SLOT:
557c478bd9Sstevel@tonic-gate /* LINTED */
567c478bd9Sstevel@tonic-gate _oaddr = (unsigned long *)oaddr;
577c478bd9Sstevel@tonic-gate /* LINTED */
587c478bd9Sstevel@tonic-gate _iaddr = (unsigned long *)iaddr;
595aefb655Srie
607c478bd9Sstevel@tonic-gate if (_iaddr) {
617c478bd9Sstevel@tonic-gate *_oaddr++ = *_iaddr++;
627c478bd9Sstevel@tonic-gate *_oaddr++ = *_iaddr++;
637c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr;
647c478bd9Sstevel@tonic-gate } else {
657c478bd9Sstevel@tonic-gate *_oaddr++ = 0;
667c478bd9Sstevel@tonic-gate *_oaddr++ = 0;
677c478bd9Sstevel@tonic-gate *_oaddr = 0;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate break;
707c478bd9Sstevel@tonic-gate default:
717c478bd9Sstevel@tonic-gate rep = &reloc_table[rtype];
727c478bd9Sstevel@tonic-gate if (iaddr)
737c478bd9Sstevel@tonic-gate (void) memcpy(oaddr, iaddr, rep->re_fsize);
747c478bd9Sstevel@tonic-gate else
757c478bd9Sstevel@tonic-gate (void) memset(oaddr, 0, rep->re_fsize);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Copy a relocation record and increment its value. The record must reflect
817c478bd9Sstevel@tonic-gate * the new address to which this image is fixed.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
847c478bd9Sstevel@tonic-gate void
inc_reloc(void * vnrel,void * vorel,Reloc * reloc,uchar_t * oaddr,uchar_t * iaddr)855aefb655Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
865aefb655Srie uchar_t *iaddr)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate Rela *nrel = vnrel;
897c478bd9Sstevel@tonic-gate Rela *orel = vorel;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate *nrel = *orel;
927c478bd9Sstevel@tonic-gate nrel->r_offset += reloc->r_value;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Clear a relocation record. The relocation has been applied to the image and
977c478bd9Sstevel@tonic-gate * thus the relocation must not occur again.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate void
clear_reloc(void * vrel)1007c478bd9Sstevel@tonic-gate clear_reloc(void *vrel)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate Rela *rel = vrel;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate rel->r_offset = 0;
1057c478bd9Sstevel@tonic-gate rel->r_info = ELF_R_INFO(0, R_SPARC_NONE);
1067c478bd9Sstevel@tonic-gate rel->r_addend = 0;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Apply a relocation to an image being built from an input file. Use the
1117c478bd9Sstevel@tonic-gate * runtime linkers routines to do the necessary magic.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate void
apply_reloc(void * vrel,Reloc * reloc,const char * name,uchar_t * oaddr,Rt_map * lmp)1145aefb655Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
1155aefb655Srie Rt_map *lmp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate Rela *rel = vrel;
118ba2be530Sab196087 Xword type = ELF_R_TYPE(rel->r_info, M_MACH);
1197c478bd9Sstevel@tonic-gate Xword value = reloc->r_value + rel->r_addend;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (type == R_SPARC_JMP_SLOT) {
1227c478bd9Sstevel@tonic-gate uintptr_t addr, vaddr;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate if (FLAGS(lmp) & FLG_RT_FIXED)
1257c478bd9Sstevel@tonic-gate vaddr = 0;
1267c478bd9Sstevel@tonic-gate else
1277c478bd9Sstevel@tonic-gate vaddr = ADDR(lmp);
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate addr = (uintptr_t)oaddr - rel->r_offset;
1307c478bd9Sstevel@tonic-gate /* LINTED */
131*80148899SSurya Prakki (void) elf_plt_write((uintptr_t)addr, vaddr, rel,
1327c478bd9Sstevel@tonic-gate (uintptr_t)value, reloc->r_pltndx);
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate } else if (type == R_SPARC_COPY) {
1357c478bd9Sstevel@tonic-gate (void) memcpy((void *)oaddr, (void *)value,
1367c478bd9Sstevel@tonic-gate (size_t)reloc->r_size);
1377c478bd9Sstevel@tonic-gate } else {
1387c478bd9Sstevel@tonic-gate if (IS_EXTOFFSET(type))
1397c478bd9Sstevel@tonic-gate value += ELF_R_TYPE_DATA(rel->r_info);
140f3324781Sab196087 (void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name,
1415aefb655Srie LIST(lmp));
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate }
144