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. 247c478bd9Sstevel@tonic-gate * 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 */ 385aefb655Srie /* ARGSUSED3 */ 397c478bd9Sstevel@tonic-gate void 405aefb655Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc) 417c478bd9Sstevel@tonic-gate { 425aefb655Srie Rela *rel = vrel; 43ba2be530Sab196087 Xword rtype = ELF_R_TYPE(rel->r_info, M_MACH); 447c478bd9Sstevel@tonic-gate 455aefb655Srie switch (rtype) { 465aefb655Srie case R_AMD64_NONE: 477c478bd9Sstevel@tonic-gate break; 485aefb655Srie case R_AMD64_COPY: 497c478bd9Sstevel@tonic-gate (void) memset((void *)oaddr, 0, (size_t)reloc->r_size); 507c478bd9Sstevel@tonic-gate break; 515aefb655Srie case R_AMD64_JUMP_SLOT: 5265b18088Sab196087 { 535aefb655Srie /* LINTED */ 5465b18088Sab196087 ulong_t *_oaddr = (ulong_t *)oaddr; 555aefb655Srie /* LINTED */ 5665b18088Sab196087 ulong_t *_iaddr = (ulong_t *)iaddr; 577c478bd9Sstevel@tonic-gate 5865b18088Sab196087 if (_iaddr) 5965b18088Sab196087 *_oaddr = *_iaddr + reloc->r_value; 6065b18088Sab196087 else 6165b18088Sab196087 *_oaddr = reloc->r_value; 625aefb655Srie } 637c478bd9Sstevel@tonic-gate break; 645aefb655Srie default: 6565b18088Sab196087 { 6665b18088Sab196087 size_t re_fsize = reloc_table[rtype].re_fsize; 6765b18088Sab196087 685aefb655Srie if (iaddr) 6965b18088Sab196087 (void) memcpy(oaddr, iaddr, re_fsize); 705aefb655Srie else 7165b18088Sab196087 (void) memset(oaddr, 0, re_fsize); 7265b18088Sab196087 } 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Copy a relocation record and increment its value. The record must reflect 7865b18088Sab196087 * the new address to which this image is fixed. Note that .got entries 7965b18088Sab196087 * associated with .plt's must be fixed to the new base address. 807c478bd9Sstevel@tonic-gate */ 815aefb655Srie /* ARGSUSED3 */ 827c478bd9Sstevel@tonic-gate void 835aefb655Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr, 845aefb655Srie uchar_t *iaddr) 857c478bd9Sstevel@tonic-gate { 865aefb655Srie Rela *nrel = vnrel; 875aefb655Srie Rela *orel = vorel; 887c478bd9Sstevel@tonic-gate 8965b18088Sab196087 if (ELF_R_TYPE(nrel->r_info, M_MACH) == R_AMD64_JUMP_SLOT) { 9065b18088Sab196087 /* LINTED */ 9165b18088Sab196087 ulong_t *_oaddr = (ulong_t *)oaddr; 9265b18088Sab196087 /* LINTED */ 9365b18088Sab196087 ulong_t *_iaddr = (ulong_t *)iaddr; 9465b18088Sab196087 9565b18088Sab196087 if (_iaddr) 9665b18088Sab196087 *_oaddr = *_iaddr + reloc->r_value; 9765b18088Sab196087 else 9865b18088Sab196087 *_oaddr = reloc->r_value; 9965b18088Sab196087 } 10065b18088Sab196087 1017c478bd9Sstevel@tonic-gate *nrel = *orel; 1027c478bd9Sstevel@tonic-gate nrel->r_offset += reloc->r_value; 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * Clear a relocation record. The relocation has been applied to the image and 1077c478bd9Sstevel@tonic-gate * thus the relocation must not occur again. 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate void 1107c478bd9Sstevel@tonic-gate clear_reloc(void *vrel) 1117c478bd9Sstevel@tonic-gate { 1125aefb655Srie Rela *rel = vrel; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate rel->r_offset = 0; 1155aefb655Srie rel->r_info = ELF_R_INFO(0, R_AMD64_NONE); 1165aefb655Srie rel->r_addend = 0; 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Apply a relocation to an image being built from an input file. Use the 1217c478bd9Sstevel@tonic-gate * runtime linkers routines to do the necessary magic. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate void 1245aefb655Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr, 1255aefb655Srie Rt_map *lmp) 1267c478bd9Sstevel@tonic-gate { 1275aefb655Srie Rela *rel = vrel; 128ba2be530Sab196087 Xword type = ELF_R_TYPE(rel->r_info, M_MACH); 1295aefb655Srie Xword value = reloc->r_value + rel->r_addend; 1307c478bd9Sstevel@tonic-gate 1315aefb655Srie if (type == R_AMD64_JUMP_SLOT) { 1327c478bd9Sstevel@tonic-gate uintptr_t addr, vaddr; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (FLAGS(lmp) & FLG_RT_FIXED) 1357c478bd9Sstevel@tonic-gate vaddr = 0; 1367c478bd9Sstevel@tonic-gate else 1377c478bd9Sstevel@tonic-gate vaddr = ADDR(lmp); 1385aefb655Srie 1397c478bd9Sstevel@tonic-gate addr = (uintptr_t)oaddr - rel->r_offset; 1407c478bd9Sstevel@tonic-gate /* LINTED */ 141*80148899SSurya Prakki (void) elf_plt_write((uintptr_t)addr, vaddr, rel, 1427c478bd9Sstevel@tonic-gate (uintptr_t)value, reloc->r_pltndx); 1435aefb655Srie 1445aefb655Srie } else if (type == R_AMD64_COPY) { 1457c478bd9Sstevel@tonic-gate (void) memcpy((void *)oaddr, (void *)value, 1467c478bd9Sstevel@tonic-gate (size_t)reloc->r_size); 1477c478bd9Sstevel@tonic-gate } else { 148f3324781Sab196087 (void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name, 1495aefb655Srie LIST(lmp)); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate } 152