1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2000-2001 by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <string.h> 29 #include "machdep.h" 30 #include "reloc.h" 31 #include "_librtld.h" 32 #include "_elf.h" 33 34 35 /* 36 * Undo relocations that have been applied to a memory image. Basically this 37 * involves copying the original files relocation offset into the new image 38 * being created. 39 */ 40 void 41 undo_reloc(void *vrel, unsigned char *oaddr, unsigned char *iaddr, 42 Reloc *reloc) 43 { 44 Rel *rel = vrel; 45 /* LINTED */ 46 unsigned long *_oaddr = (unsigned long *)oaddr; 47 /* LINTED */ 48 unsigned long *_iaddr = (unsigned long *)iaddr; 49 50 switch (ELF_R_TYPE(rel->r_info)) { 51 case R_386_NONE: 52 break; 53 54 case R_386_COPY: 55 (void) memset((void *)oaddr, 0, (size_t)reloc->r_size); 56 break; 57 58 case R_386_JMP_SLOT: 59 if (_iaddr) 60 *_oaddr = *_iaddr + reloc->r_value; 61 else 62 *_oaddr = reloc->r_value; 63 break; 64 65 default: 66 if (_iaddr) 67 *_oaddr = *_iaddr; 68 else 69 *_oaddr = 0; 70 break; 71 } 72 } 73 74 75 /* 76 * Copy a relocation record and increment its value. The record must reflect 77 * the new address to which this image is fixed. Note that .got entries 78 * associated with .plt's must be fixed to the new base address. 79 */ 80 void 81 inc_reloc(void *vnrel, void *vorel, Reloc *reloc, unsigned char *oaddr, 82 unsigned char *iaddr) 83 { 84 Rel *nrel = vnrel; 85 Rel *orel = vorel; 86 /* LINTED */ 87 unsigned long *_oaddr = (unsigned long *)oaddr; 88 /* LINTED */ 89 unsigned long *_iaddr = (unsigned long *)iaddr; 90 91 if (ELF_R_TYPE(nrel->r_info) == R_386_JMP_SLOT) { 92 if (_iaddr) 93 *_oaddr = *_iaddr + reloc->r_value; 94 else 95 *_oaddr = reloc->r_value; 96 } 97 98 *nrel = *orel; 99 nrel->r_offset += reloc->r_value; 100 } 101 102 103 /* 104 * Clear a relocation record. The relocation has been applied to the image and 105 * thus the relocation must not occur again. 106 */ 107 void 108 clear_reloc(void * vrel) 109 { 110 Rel * rel = vrel; 111 112 rel->r_offset = 0; 113 rel->r_info = ELF_R_INFO(0, R_386_NONE); 114 } 115 116 117 /* 118 * Apply a relocation to an image being built from an input file. Use the 119 * runtime linkers routines to do the necessary magic. 120 */ 121 void 122 apply_reloc(void *vrel, Reloc *reloc, const char *name, 123 unsigned char *oaddr, Rt_map *lmp) 124 { 125 Rel *rel = vrel; 126 Byte type = ELF_R_TYPE(rel->r_info); 127 Word value = reloc->r_value; 128 129 if (type == R_386_JMP_SLOT) { 130 uintptr_t addr, vaddr; 131 132 if (FLAGS(lmp) & FLG_RT_FIXED) 133 vaddr = 0; 134 else 135 vaddr = ADDR(lmp); 136 addr = (uintptr_t)oaddr - rel->r_offset; 137 /* LINTED */ 138 elf_plt_write((uintptr_t)addr, vaddr, rel, 139 (uintptr_t)value, reloc->r_pltndx); 140 } else if (type == R_386_COPY) { 141 (void) memcpy((void *)oaddr, (void *)value, 142 (size_t)reloc->r_size); 143 144 } else { 145 (void) do_reloc(type, oaddr, &value, reloc->r_name, name); 146 } 147 } 148