1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2000-2002 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <strings.h> 31*0Sstevel@tonic-gate #include <sys/elf.h> 32*0Sstevel@tonic-gate #include <sys/elf_SPARC.h> 33*0Sstevel@tonic-gate #include <alloca.h> 34*0Sstevel@tonic-gate #include "_rtld.h" 35*0Sstevel@tonic-gate #include "_elf.h" 36*0Sstevel@tonic-gate #include "msg.h" 37*0Sstevel@tonic-gate #include "conv.h" 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * Matrix of legal combinations of usage of a given register: 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * Obj1\Obj2 Scratch Named 44*0Sstevel@tonic-gate * Scratch OK NO 45*0Sstevel@tonic-gate * Named NO * 46*0Sstevel@tonic-gate * 47*0Sstevel@tonic-gate * * OK if the symbols are identical, NO if they are not. Two symbols 48*0Sstevel@tonic-gate * are identical if and only if one of the following is true: 49*0Sstevel@tonic-gate * A. They are both global and have the same name. 50*0Sstevel@tonic-gate * B. They are both local, have the same name, and are defined in 51*0Sstevel@tonic-gate * the same object. (Note that a local symbol in one object is 52*0Sstevel@tonic-gate * never identical to a local symbol in another object, even if the 53*0Sstevel@tonic-gate * name is the same.) 54*0Sstevel@tonic-gate * 55*0Sstevel@tonic-gate * Matrix of legal combinations of st_shndx for the same register symbol: 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate * Obj1\Obj2 UNDEF ABS 58*0Sstevel@tonic-gate * UNDEF OK OK 59*0Sstevel@tonic-gate * ABS OK NO 60*0Sstevel@tonic-gate */ 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* 63*0Sstevel@tonic-gate * Test the compatiblity of two register symbols, 0 pass, >0 fail 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate static uintptr_t 66*0Sstevel@tonic-gate check_regsyms(Sym * sym1, const char * name1, Sym * sym2, const char * name2) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate if ((sym1->st_name == 0) && (sym2->st_name == 0)) 69*0Sstevel@tonic-gate return (0); /* scratches are always compatible */ 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate if ((ELF_ST_BIND(sym1->st_info) == STB_LOCAL) || 72*0Sstevel@tonic-gate (ELF_ST_BIND(sym2->st_info) == STB_LOCAL)) { 73*0Sstevel@tonic-gate if (sym1->st_value == sym2->st_value) 74*0Sstevel@tonic-gate return (1); /* local symbol incompat */ 75*0Sstevel@tonic-gate return (0); /* no other prob from locals */ 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate if (sym1->st_value == sym2->st_value) { 79*0Sstevel@tonic-gate /* NOTE this just avoids strcmp */ 80*0Sstevel@tonic-gate if ((sym1->st_name == 0) || (sym2->st_name == 0)) 81*0Sstevel@tonic-gate return (2); /* can't match scratch to named */ 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate if (strcmp(name1, name2) != 0) 84*0Sstevel@tonic-gate return (4); /* diff name, same register value */ 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate if ((sym1->st_shndx == SHN_ABS) && (sym2->st_shndx == SHN_ABS)) 87*0Sstevel@tonic-gate return (3); /* multiply defined */ 88*0Sstevel@tonic-gate } else if (strcmp(name1, name2) == 0) 89*0Sstevel@tonic-gate return (5); /* same name, diff register value */ 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate return (0); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate int 95*0Sstevel@tonic-gate elf_regsyms(Rt_map * lmp) 96*0Sstevel@tonic-gate { 97*0Sstevel@tonic-gate Dyn * dyn; 98*0Sstevel@tonic-gate Sym * symdef; 99*0Sstevel@tonic-gate ulong_t rsymndx; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate /* 102*0Sstevel@tonic-gate * Scan through the .dynamic section of this object looking for all 103*0Sstevel@tonic-gate * DT_REGISTER entries. For each DT_REGISTER entry found identify the 104*0Sstevel@tonic-gate * register symbol it identifies and confirm that it doesn't conflict 105*0Sstevel@tonic-gate * with any other register symbols. 106*0Sstevel@tonic-gate */ 107*0Sstevel@tonic-gate for (dyn = DYN(lmp); dyn->d_tag != DT_NULL; dyn++) { 108*0Sstevel@tonic-gate Reglist * rp; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate if ((dyn->d_tag != DT_SPARC_REGISTER) && 111*0Sstevel@tonic-gate (dyn->d_tag != DT_DEPRECATED_SPARC_REGISTER)) 112*0Sstevel@tonic-gate continue; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /* 115*0Sstevel@tonic-gate * Get the local symbol table entry. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate rsymndx = dyn->d_un.d_val; 118*0Sstevel@tonic-gate symdef = (Sym *)((unsigned long)SYMTAB(lmp) + 119*0Sstevel@tonic-gate (rsymndx * SYMENT(lmp))); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate for (rp = reglist; rp; rp = rp->rl_next) { 122*0Sstevel@tonic-gate const char * str, * sym1, * sym2; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate if (rp->rl_sym == symdef) { 125*0Sstevel@tonic-gate /* 126*0Sstevel@tonic-gate * Same symbol definition - everything is a-ok. 127*0Sstevel@tonic-gate */ 128*0Sstevel@tonic-gate return (1); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate sym1 = (STRTAB(rp->rl_lmp) + rp->rl_sym->st_name); 132*0Sstevel@tonic-gate sym2 = (STRTAB(lmp) + symdef->st_name); 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate if (check_regsyms(rp->rl_sym, sym1, symdef, sym2) == 0) 135*0Sstevel@tonic-gate continue; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate if ((str = demangle(sym1)) != sym1) { 138*0Sstevel@tonic-gate char * _str = alloca(strlen(str) + 1); 139*0Sstevel@tonic-gate (void) strcpy(_str, str); 140*0Sstevel@tonic-gate sym1 = (const char *)_str; 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate sym2 = demangle(sym2); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) { 145*0Sstevel@tonic-gate (void) printf(MSG_INTL(MSG_LDD_REG_SYMCONF), 146*0Sstevel@tonic-gate conv_sym_SPARC_value_str(symdef->st_value), 147*0Sstevel@tonic-gate NAME(rp->rl_lmp), sym1, NAME(lmp), sym2); 148*0Sstevel@tonic-gate } else { 149*0Sstevel@tonic-gate eprintf(ERR_FATAL, MSG_INTL(MSG_REG_SYMCONF), 150*0Sstevel@tonic-gate conv_sym_SPARC_value_str(symdef->st_value), 151*0Sstevel@tonic-gate NAME(rp->rl_lmp), sym1, NAME(lmp), sym2); 152*0Sstevel@tonic-gate return (0); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate if ((rp = calloc(sizeof (Reglist), 1)) == (Reglist *)0) 156*0Sstevel@tonic-gate return (0); 157*0Sstevel@tonic-gate rp->rl_lmp = lmp; 158*0Sstevel@tonic-gate rp->rl_sym = symdef; 159*0Sstevel@tonic-gate rp->rl_next = reglist; 160*0Sstevel@tonic-gate reglist = rp; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate return (1); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * When the relocation loop realizes that it's dealing with relative 168*0Sstevel@tonic-gate * relocations in a shared object, it breaks into this tighter loop 169*0Sstevel@tonic-gate * as an optimization. 170*0Sstevel@tonic-gate */ 171*0Sstevel@tonic-gate ulong_t 172*0Sstevel@tonic-gate elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz, 173*0Sstevel@tonic-gate ulong_t basebgn, ulong_t etext, ulong_t emap) 174*0Sstevel@tonic-gate { 175*0Sstevel@tonic-gate ulong_t roffset = ((Rela *) relbgn)->r_offset; 176*0Sstevel@tonic-gate Byte rtype; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate do { 179*0Sstevel@tonic-gate roffset += basebgn; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* 182*0Sstevel@tonic-gate * If this relocation is against an address not mapped in, 183*0Sstevel@tonic-gate * then break out of the relative relocation loop, falling 184*0Sstevel@tonic-gate * back on the main relocation loop. 185*0Sstevel@tonic-gate */ 186*0Sstevel@tonic-gate if (roffset < etext || roffset > emap) 187*0Sstevel@tonic-gate break; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate /* 190*0Sstevel@tonic-gate * Perform the actual relocation. 191*0Sstevel@tonic-gate */ 192*0Sstevel@tonic-gate *((ulong_t *)roffset) += 193*0Sstevel@tonic-gate basebgn + (long)(((Rela *)relbgn)->r_addend); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate relbgn += relsiz; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate if (relbgn >= relend) 198*0Sstevel@tonic-gate break; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate rtype = (Byte)ELF_R_TYPE(((Rela *)relbgn)->r_info); 201*0Sstevel@tonic-gate roffset = ((Rela *)relbgn)->r_offset; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate } while (rtype == R_SPARC_RELATIVE); 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate return (relbgn); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* 209*0Sstevel@tonic-gate * This is the tightest loop for RELATIVE relocations for those 210*0Sstevel@tonic-gate * objects built with the DT_RELACOUNT .dynamic entry. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate ulong_t 213*0Sstevel@tonic-gate elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz, 214*0Sstevel@tonic-gate ulong_t basebgn) 215*0Sstevel@tonic-gate { 216*0Sstevel@tonic-gate ulong_t roffset = ((Rela *) relbgn)->r_offset; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate for (; relacount; relacount--) { 219*0Sstevel@tonic-gate roffset += basebgn; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* 222*0Sstevel@tonic-gate * Perform the actual relocation. 223*0Sstevel@tonic-gate */ 224*0Sstevel@tonic-gate *((ulong_t *)roffset) = 225*0Sstevel@tonic-gate basebgn + (long)(((Rela *)relbgn)->r_addend); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate relbgn += relsiz; 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate roffset = ((Rela *)relbgn)->r_offset; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate return (relbgn); 233*0Sstevel@tonic-gate } 234