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