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*12449SRod.Evans@Sun.COM * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <strings.h>
280Sstevel@tonic-gate #include <sys/elf.h>
290Sstevel@tonic-gate #include <sys/elf_SPARC.h>
300Sstevel@tonic-gate #include <alloca.h>
310Sstevel@tonic-gate #include "_rtld.h"
320Sstevel@tonic-gate #include "_elf.h"
330Sstevel@tonic-gate #include "msg.h"
340Sstevel@tonic-gate #include "conv.h"
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * Matrix of legal combinations of usage of a given register:
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * Obj1\Obj2 Scratch Named
410Sstevel@tonic-gate * Scratch OK NO
420Sstevel@tonic-gate * Named NO *
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * * OK if the symbols are identical, NO if they are not. Two symbols
450Sstevel@tonic-gate * are identical if and only if one of the following is true:
460Sstevel@tonic-gate * A. They are both global and have the same name.
470Sstevel@tonic-gate * B. They are both local, have the same name, and are defined in
480Sstevel@tonic-gate * the same object. (Note that a local symbol in one object is
490Sstevel@tonic-gate * never identical to a local symbol in another object, even if the
500Sstevel@tonic-gate * name is the same.)
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * Matrix of legal combinations of st_shndx for the same register symbol:
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * Obj1\Obj2 UNDEF ABS
550Sstevel@tonic-gate * UNDEF OK OK
560Sstevel@tonic-gate * ABS OK NO
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * Test the compatiblity of two register symbols, 0 pass, >0 fail
610Sstevel@tonic-gate */
620Sstevel@tonic-gate static uintptr_t
check_regsyms(Sym * sym1,const char * name1,Sym * sym2,const char * name2)631618Srie check_regsyms(Sym *sym1, const char *name1, Sym *sym2, const char *name2)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate if ((sym1->st_name == 0) && (sym2->st_name == 0))
660Sstevel@tonic-gate return (0); /* scratches are always compatible */
670Sstevel@tonic-gate
680Sstevel@tonic-gate if ((ELF_ST_BIND(sym1->st_info) == STB_LOCAL) ||
690Sstevel@tonic-gate (ELF_ST_BIND(sym2->st_info) == STB_LOCAL)) {
700Sstevel@tonic-gate if (sym1->st_value == sym2->st_value)
710Sstevel@tonic-gate return (1); /* local symbol incompat */
720Sstevel@tonic-gate return (0); /* no other prob from locals */
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (sym1->st_value == sym2->st_value) {
760Sstevel@tonic-gate /* NOTE this just avoids strcmp */
770Sstevel@tonic-gate if ((sym1->st_name == 0) || (sym2->st_name == 0))
780Sstevel@tonic-gate return (2); /* can't match scratch to named */
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (strcmp(name1, name2) != 0)
810Sstevel@tonic-gate return (4); /* diff name, same register value */
820Sstevel@tonic-gate
830Sstevel@tonic-gate if ((sym1->st_shndx == SHN_ABS) && (sym2->st_shndx == SHN_ABS))
840Sstevel@tonic-gate return (3); /* multiply defined */
850Sstevel@tonic-gate } else if (strcmp(name1, name2) == 0)
860Sstevel@tonic-gate return (5); /* same name, diff register value */
870Sstevel@tonic-gate
880Sstevel@tonic-gate return (0);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate int
elf_regsyms(Rt_map * lmp)928598SRod.Evans@Sun.COM elf_regsyms(Rt_map *lmp)
930Sstevel@tonic-gate {
948598SRod.Evans@Sun.COM Dyn *dyn;
95*12449SRod.Evans@Sun.COM Dyninfo *dip;
968598SRod.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 */
105*12449SRod.Evans@Sun.COM for (dyn = DYN(lmp), dip = DYNINFO(lmp);
106*12449SRod.Evans@Sun.COM !(dip->di_flags & FLG_DI_IGNORE); dyn++, dip++) {
1078598SRod.Evans@Sun.COM Reglist *rp;
1080Sstevel@tonic-gate
109*12449SRod.Evans@Sun.COM if ((dip->di_flags & FLG_DI_REGISTER) == 0)
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 }
1578598SRod.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