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*f3324781Sab196087 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 245aefb655Srie * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <string.h> 297c478bd9Sstevel@tonic-gate #include "machdep.h" 307c478bd9Sstevel@tonic-gate #include "reloc.h" 317c478bd9Sstevel@tonic-gate #include "_librtld.h" 327c478bd9Sstevel@tonic-gate #include "_elf.h" 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Undo relocations that have been applied to a memory image. Basically this 367c478bd9Sstevel@tonic-gate * involves copying the original files relocation offset into the new image 377c478bd9Sstevel@tonic-gate * being created. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate void 405aefb655Srie undo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc) 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate Rel *rel = vrel; 437c478bd9Sstevel@tonic-gate /* LINTED */ 445aefb655Srie ulong_t *_oaddr = (ulong_t *)oaddr; 457c478bd9Sstevel@tonic-gate /* LINTED */ 465aefb655Srie ulong_t *_iaddr = (ulong_t *)iaddr; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate switch (ELF_R_TYPE(rel->r_info)) { 497c478bd9Sstevel@tonic-gate case R_386_NONE: 507c478bd9Sstevel@tonic-gate break; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate case R_386_COPY: 537c478bd9Sstevel@tonic-gate (void) memset((void *)oaddr, 0, (size_t)reloc->r_size); 547c478bd9Sstevel@tonic-gate break; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate case R_386_JMP_SLOT: 577c478bd9Sstevel@tonic-gate if (_iaddr) 587c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value; 597c478bd9Sstevel@tonic-gate else 607c478bd9Sstevel@tonic-gate *_oaddr = reloc->r_value; 617c478bd9Sstevel@tonic-gate break; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate default: 647c478bd9Sstevel@tonic-gate if (_iaddr) 657c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr; 667c478bd9Sstevel@tonic-gate else 677c478bd9Sstevel@tonic-gate *_oaddr = 0; 687c478bd9Sstevel@tonic-gate break; 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * Copy a relocation record and increment its value. The record must reflect 747c478bd9Sstevel@tonic-gate * the new address to which this image is fixed. Note that .got entries 757c478bd9Sstevel@tonic-gate * associated with .plt's must be fixed to the new base address. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate void 785aefb655Srie inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr, 795aefb655Srie uchar_t *iaddr) 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate Rel *nrel = vnrel; 827c478bd9Sstevel@tonic-gate Rel *orel = vorel; 837c478bd9Sstevel@tonic-gate /* LINTED */ 845aefb655Srie ulong_t *_oaddr = (ulong_t *)oaddr; 857c478bd9Sstevel@tonic-gate /* LINTED */ 865aefb655Srie ulong_t *_iaddr = (ulong_t *)iaddr; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if (ELF_R_TYPE(nrel->r_info) == R_386_JMP_SLOT) { 897c478bd9Sstevel@tonic-gate if (_iaddr) 907c478bd9Sstevel@tonic-gate *_oaddr = *_iaddr + reloc->r_value; 917c478bd9Sstevel@tonic-gate else 927c478bd9Sstevel@tonic-gate *_oaddr = reloc->r_value; 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate *nrel = *orel; 967c478bd9Sstevel@tonic-gate nrel->r_offset += reloc->r_value; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Clear a relocation record. The relocation has been applied to the image and 1017c478bd9Sstevel@tonic-gate * thus the relocation must not occur again. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate void 1047c478bd9Sstevel@tonic-gate clear_reloc(void *vrel) 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate Rel *rel = vrel; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate rel->r_offset = 0; 1097c478bd9Sstevel@tonic-gate rel->r_info = ELF_R_INFO(0, R_386_NONE); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * Apply a relocation to an image being built from an input file. Use the 1147c478bd9Sstevel@tonic-gate * runtime linkers routines to do the necessary magic. 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate void 1175aefb655Srie apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr, 1185aefb655Srie Rt_map *lmp) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate Rel *rel = vrel; 1215aefb655Srie Xword type = ELF_R_TYPE(rel->r_info); 1227c478bd9Sstevel@tonic-gate Word value = reloc->r_value; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (type == R_386_JMP_SLOT) { 1257c478bd9Sstevel@tonic-gate uintptr_t addr, vaddr; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (FLAGS(lmp) & FLG_RT_FIXED) 1287c478bd9Sstevel@tonic-gate vaddr = 0; 1297c478bd9Sstevel@tonic-gate else 1307c478bd9Sstevel@tonic-gate vaddr = ADDR(lmp); 1317c478bd9Sstevel@tonic-gate addr = (uintptr_t)oaddr - rel->r_offset; 1327c478bd9Sstevel@tonic-gate /* LINTED */ 1337c478bd9Sstevel@tonic-gate elf_plt_write((uintptr_t)addr, vaddr, rel, 1347c478bd9Sstevel@tonic-gate (uintptr_t)value, reloc->r_pltndx); 1355aefb655Srie 1367c478bd9Sstevel@tonic-gate } else if (type == R_386_COPY) { 1377c478bd9Sstevel@tonic-gate (void) memcpy((void *)oaddr, (void *)value, 1387c478bd9Sstevel@tonic-gate (size_t)reloc->r_size); 1397c478bd9Sstevel@tonic-gate } else { 140*f3324781Sab196087 (void) do_reloc_rtld(type, oaddr, &value, reloc->r_name, name, 1415aefb655Srie LIST(lmp)); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate } 144