10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51618Srie * Common Development and Distribution License (the "License"). 61618Srie * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211618Srie 220Sstevel@tonic-gate /* 23*8598SRod.Evans@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <stdio.h> 280Sstevel@tonic-gate #include <strings.h> 290Sstevel@tonic-gate #include <sys/elf.h> 300Sstevel@tonic-gate #include <sys/elf_SPARC.h> 310Sstevel@tonic-gate #include <alloca.h> 320Sstevel@tonic-gate #include "_rtld.h" 330Sstevel@tonic-gate #include "_elf.h" 340Sstevel@tonic-gate #include "msg.h" 350Sstevel@tonic-gate #include "conv.h" 360Sstevel@tonic-gate 370Sstevel@tonic-gate /* 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * Matrix of legal combinations of usage of a given register: 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * Obj1\Obj2 Scratch Named 420Sstevel@tonic-gate * Scratch OK NO 430Sstevel@tonic-gate * Named NO * 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * * OK if the symbols are identical, NO if they are not. Two symbols 460Sstevel@tonic-gate * are identical if and only if one of the following is true: 470Sstevel@tonic-gate * A. They are both global and have the same name. 480Sstevel@tonic-gate * B. They are both local, have the same name, and are defined in 490Sstevel@tonic-gate * the same object. (Note that a local symbol in one object is 500Sstevel@tonic-gate * never identical to a local symbol in another object, even if the 510Sstevel@tonic-gate * name is the same.) 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * Matrix of legal combinations of st_shndx for the same register symbol: 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Obj1\Obj2 UNDEF ABS 560Sstevel@tonic-gate * UNDEF OK OK 570Sstevel@tonic-gate * ABS OK NO 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Test the compatiblity of two register symbols, 0 pass, >0 fail 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate static uintptr_t 641618Srie check_regsyms(Sym *sym1, const char *name1, Sym *sym2, const char *name2) 650Sstevel@tonic-gate { 660Sstevel@tonic-gate if ((sym1->st_name == 0) && (sym2->st_name == 0)) 670Sstevel@tonic-gate return (0); /* scratches are always compatible */ 680Sstevel@tonic-gate 690Sstevel@tonic-gate if ((ELF_ST_BIND(sym1->st_info) == STB_LOCAL) || 700Sstevel@tonic-gate (ELF_ST_BIND(sym2->st_info) == STB_LOCAL)) { 710Sstevel@tonic-gate if (sym1->st_value == sym2->st_value) 720Sstevel@tonic-gate return (1); /* local symbol incompat */ 730Sstevel@tonic-gate return (0); /* no other prob from locals */ 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate if (sym1->st_value == sym2->st_value) { 770Sstevel@tonic-gate /* NOTE this just avoids strcmp */ 780Sstevel@tonic-gate if ((sym1->st_name == 0) || (sym2->st_name == 0)) 790Sstevel@tonic-gate return (2); /* can't match scratch to named */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate if (strcmp(name1, name2) != 0) 820Sstevel@tonic-gate return (4); /* diff name, same register value */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate if ((sym1->st_shndx == SHN_ABS) && (sym2->st_shndx == SHN_ABS)) 850Sstevel@tonic-gate return (3); /* multiply defined */ 860Sstevel@tonic-gate } else if (strcmp(name1, name2) == 0) 870Sstevel@tonic-gate return (5); /* same name, diff register value */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate return (0); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate int 93*8598SRod.Evans@Sun.COM elf_regsyms(Rt_map *lmp) 940Sstevel@tonic-gate { 95*8598SRod.Evans@Sun.COM Dyn *dyn; 96*8598SRod.Evans@Sun.COM Sym *symdef; 970Sstevel@tonic-gate ulong_t rsymndx; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Scan through the .dynamic section of this object looking for all 1010Sstevel@tonic-gate * DT_REGISTER entries. For each DT_REGISTER entry found identify the 1020Sstevel@tonic-gate * register symbol it identifies and confirm that it doesn't conflict 1030Sstevel@tonic-gate * with any other register symbols. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate for (dyn = DYN(lmp); dyn->d_tag != DT_NULL; dyn++) { 106*8598SRod.Evans@Sun.COM Reglist *rp; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if ((dyn->d_tag != DT_SPARC_REGISTER) && 1090Sstevel@tonic-gate (dyn->d_tag != DT_DEPRECATED_SPARC_REGISTER)) 1100Sstevel@tonic-gate continue; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Get the local symbol table entry. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate rsymndx = dyn->d_un.d_val; 1160Sstevel@tonic-gate symdef = (Sym *)((unsigned long)SYMTAB(lmp) + 1174734Sab196087 (rsymndx * SYMENT(lmp))); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate for (rp = reglist; rp; rp = rp->rl_next) { 1204734Sab196087 Conv_inv_buf_t inv_buf; 1211618Srie const char *str, *sym1, *sym2; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate if (rp->rl_sym == symdef) { 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * Same symbol definition - everything is a-ok. 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate return (1); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate sym1 = (STRTAB(rp->rl_lmp) + rp->rl_sym->st_name); 1310Sstevel@tonic-gate sym2 = (STRTAB(lmp) + symdef->st_name); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate if (check_regsyms(rp->rl_sym, sym1, symdef, sym2) == 0) 1340Sstevel@tonic-gate continue; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate if ((str = demangle(sym1)) != sym1) { 1371618Srie char *_str = alloca(strlen(str) + 1); 1380Sstevel@tonic-gate (void) strcpy(_str, str); 1390Sstevel@tonic-gate sym1 = (const char *)_str; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate sym2 = demangle(sym2); 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate if (LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) { 1440Sstevel@tonic-gate (void) printf(MSG_INTL(MSG_LDD_REG_SYMCONF), 1454734Sab196087 conv_sym_SPARC_value(symdef->st_value, 1464734Sab196087 0, &inv_buf), NAME(rp->rl_lmp), 1474734Sab196087 sym1, NAME(lmp), sym2); 1480Sstevel@tonic-gate } else { 1491618Srie eprintf(LIST(lmp), ERR_FATAL, 1501618Srie MSG_INTL(MSG_REG_SYMCONF), 1514734Sab196087 conv_sym_SPARC_value(symdef->st_value, 1524734Sab196087 0, &inv_buf), NAME(rp->rl_lmp), 1534734Sab196087 sym1, NAME(lmp), sym2); 1540Sstevel@tonic-gate return (0); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate } 157*8598SRod.Evans@Sun.COM if ((rp = calloc(sizeof (Reglist), 1)) == NULL) 1580Sstevel@tonic-gate return (0); 1590Sstevel@tonic-gate rp->rl_lmp = lmp; 1600Sstevel@tonic-gate rp->rl_sym = symdef; 1610Sstevel@tonic-gate rp->rl_next = reglist; 1620Sstevel@tonic-gate reglist = rp; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate return (1); 1650Sstevel@tonic-gate } 166