15aefb655Srie /*
25aefb655Srie * CDDL HEADER START
35aefb655Srie *
45aefb655Srie * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
75aefb655Srie *
85aefb655Srie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95aefb655Srie * or http://www.opensolaris.org/os/licensing.
105aefb655Srie * See the License for the specific language governing permissions
115aefb655Srie * and limitations under the License.
125aefb655Srie *
135aefb655Srie * When distributing Covered Code, include this CDDL HEADER in each
145aefb655Srie * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155aefb655Srie * If applicable, add the following below this CDDL HEADER, with the
165aefb655Srie * fields enclosed by brackets "[]" replaced with your own identifying
175aefb655Srie * information: Portions Copyright [yyyy] [name of copyright owner]
185aefb655Srie *
195aefb655Srie * CDDL HEADER END
205aefb655Srie */
215aefb655Srie
225aefb655Srie /*
23*1007fd6fSAli Bahrami * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
245aefb655Srie */
25ba2be530Sab196087 #define ELF_TARGET_SPARC
26ba2be530Sab196087
275aefb655Srie #include <stdio.h>
285aefb655Srie #include <string.h>
295aefb655Srie #include <alloca.h>
305aefb655Srie #include <sys/types.h>
315aefb655Srie #include <debug.h>
325aefb655Srie #include "msg.h"
335aefb655Srie #include "_libld.h"
34ba2be530Sab196087 #include "machsym.sparc.h"
355aefb655Srie
365aefb655Srie /*
375aefb655Srie * Matrix of legal combinations of usage of a given register:
385aefb655Srie *
395aefb655Srie * Obj1 \ Obj2 Scratch Named
405aefb655Srie * Scratch OK NO
415aefb655Srie * Named NO *
425aefb655Srie *
435aefb655Srie * (*) OK if the symbols are identical, NO if they are not. Two symbols
445aefb655Srie * are identical if and only if one of the following is true:
455aefb655Srie * A. They are both global and have the same name.
465aefb655Srie * B. They are both local, have the same name, and are defined in the same
475aefb655Srie * object. (Note that a local symbol in one object is never identical to
485aefb655Srie * a local symbol in another object, even if the name is the same.)
495aefb655Srie *
505aefb655Srie * Matrix of legal combinations of st_shndx for the same register symbol:
515aefb655Srie *
525aefb655Srie * Obj1 \ Obj2 UNDEF ABS
535aefb655Srie * UNDEF OK OK
545aefb655Srie * ABS OK NO
555aefb655Srie *
565aefb655Srie */
575aefb655Srie int
ld_reg_check_sparc(Sym_desc * sdp,Sym * nsym,const char * nname,Ifl_desc * ifl,Ofl_desc * ofl)58ba2be530Sab196087 ld_reg_check_sparc(Sym_desc *sdp, Sym *nsym, const char *nname, Ifl_desc *ifl,
595aefb655Srie Ofl_desc *ofl)
605aefb655Srie {
615aefb655Srie Sym *osym = sdp->sd_sym;
625aefb655Srie const char *oname = sdp->sd_name;
63de777a60Sab196087 Conv_inv_buf_t inv_buf1, inv_buf2;
645aefb655Srie
655aefb655Srie /*
665aefb655Srie * Scratch register definitions are compatible.
675aefb655Srie */
685aefb655Srie if ((osym->st_name == 0) && (nsym->st_name == 0))
695aefb655Srie return (0);
705aefb655Srie
715aefb655Srie /*
725aefb655Srie * A local and a global, or another local is incompatible.
735aefb655Srie */
745aefb655Srie if ((ELF_ST_BIND(osym->st_info) == STB_LOCAL) ||
755aefb655Srie (ELF_ST_BIND(nsym->st_info) == STB_LOCAL)) {
765aefb655Srie if (osym->st_value == nsym->st_value) {
77de777a60Sab196087
78*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL,
795aefb655Srie MSG_INTL(MSG_SYM_INCOMPREG3),
80de777a60Sab196087 conv_sym_SPARC_value(osym->st_value, 0, &inv_buf1),
815aefb655Srie sdp->sd_file->ifl_name, demangle(oname),
825aefb655Srie ifl->ifl_name, demangle(nname));
835aefb655Srie return (1);
845aefb655Srie }
855aefb655Srie return (0);
865aefb655Srie }
875aefb655Srie
885aefb655Srie if (osym->st_value == nsym->st_value) {
895aefb655Srie /*
905aefb655Srie * A scratch register and a named register are incompatible.
915aefb655Srie * So are two different named registers.
925aefb655Srie */
935aefb655Srie if (((osym->st_name == 0) || (nsym->st_name == 0)) ||
945aefb655Srie (strcmp(oname, nname) != 0)) {
95*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_INCOMPREG1),
96de777a60Sab196087 conv_sym_SPARC_value(osym->st_value, 0, &inv_buf1),
975aefb655Srie sdp->sd_file->ifl_name, demangle(oname),
985aefb655Srie ifl->ifl_name, demangle(nname));
995aefb655Srie return (1);
1005aefb655Srie }
1015aefb655Srie
1025aefb655Srie /*
1035aefb655Srie * A multiply initialized symbol is also illegal.
1045aefb655Srie */
1055aefb655Srie if ((osym->st_shndx == SHN_ABS) &&
1065aefb655Srie (nsym->st_shndx == SHN_ABS)) {
107*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_MULTINIREG),
108de777a60Sab196087 conv_sym_SPARC_value(osym->st_value, 0, &inv_buf1),
1095aefb655Srie demangle(nname), sdp->sd_file->ifl_name,
1105aefb655Srie ifl->ifl_name);
1115aefb655Srie return (1);
1125aefb655Srie }
1135aefb655Srie
1145aefb655Srie } else if (strcmp(oname, nname) == 0) {
115*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_INCOMPREG2),
1165aefb655Srie demangle(sdp->sd_name), sdp->sd_file->ifl_name,
117de777a60Sab196087 conv_sym_SPARC_value(osym->st_value, 0, &inv_buf1),
118de777a60Sab196087 ifl->ifl_name,
119de777a60Sab196087 conv_sym_SPARC_value(nsym->st_value, 0, &inv_buf2));
1205aefb655Srie return (1);
1215aefb655Srie }
1225aefb655Srie return (0);
1235aefb655Srie }
1245aefb655Srie
1255aefb655Srie int
ld_mach_sym_typecheck_sparc(Sym_desc * sdp,Sym * nsym,Ifl_desc * ifl,Ofl_desc * ofl)126ba2be530Sab196087 ld_mach_sym_typecheck_sparc(Sym_desc *sdp, Sym *nsym, Ifl_desc *ifl,
127ba2be530Sab196087 Ofl_desc *ofl)
1285aefb655Srie {
129de777a60Sab196087 Conv_inv_buf_t inv_buf1, inv_buf2;
1305aefb655Srie Sym *osym = sdp->sd_sym;
1315aefb655Srie Byte otype = ELF_ST_TYPE(osym->st_info);
1325aefb655Srie Byte ntype = ELF_ST_TYPE(nsym->st_info);
1335aefb655Srie
1345aefb655Srie if (otype != ntype) {
1355aefb655Srie if ((otype == STT_SPARC_REGISTER) ||
1365aefb655Srie (ntype == STT_SPARC_REGISTER)) {
137*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_DIFFTYPE),
138*1007fd6fSAli Bahrami demangle(sdp->sd_name));
139*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_SYM_FILETYPES),
1405aefb655Srie sdp->sd_file->ifl_name, conv_sym_info_type(
141de777a60Sab196087 sdp->sd_file->ifl_ehdr->e_machine, otype,
142de777a60Sab196087 0, &inv_buf1), ifl->ifl_name,
1435aefb655Srie conv_sym_info_type(ifl->ifl_ehdr->e_machine,
144de777a60Sab196087 ntype, 0, &inv_buf2));
1455aefb655Srie return (1);
1465aefb655Srie }
1475aefb655Srie } else if (otype == STT_SPARC_REGISTER)
148ba2be530Sab196087 return (ld_reg_check_sparc(sdp, nsym, sdp->sd_name, ifl, ofl));
1495aefb655Srie
1505aefb655Srie return (0);
1515aefb655Srie }
1525aefb655Srie
1535aefb655Srie static const char *registers[] = { 0,
1545aefb655Srie MSG_ORIG(MSG_STO_REGISTERG1), MSG_ORIG(MSG_STO_REGISTERG2),
1555aefb655Srie MSG_ORIG(MSG_STO_REGISTERG3), MSG_ORIG(MSG_STO_REGISTERG4),
1565aefb655Srie MSG_ORIG(MSG_STO_REGISTERG5), MSG_ORIG(MSG_STO_REGISTERG6),
1575aefb655Srie MSG_ORIG(MSG_STO_REGISTERG7)
1585aefb655Srie };
1595aefb655Srie
1605aefb655Srie const char *
ld_is_regsym_sparc(Ofl_desc * ofl,Ifl_desc * ifl,Sym * sym,const char * strs,int symndx,Word shndx,const char * symsecname,sd_flag_t * flags)161ba2be530Sab196087 ld_is_regsym_sparc(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs,
162635216b6SRod Evans int symndx, Word shndx, const char *symsecname, sd_flag_t *flags)
1635aefb655Srie {
1645aefb655Srie const char *name;
1655aefb655Srie
1665aefb655Srie /*
1675aefb655Srie * Only do something if this is a register symbol.
1685aefb655Srie */
1695aefb655Srie if (ELF_ST_TYPE(sym->st_info) != STT_SPARC_REGISTER)
1705aefb655Srie return (0);
1715aefb655Srie
1725aefb655Srie /*
1735aefb655Srie * Check for bogus register number.
1745aefb655Srie */
1755aefb655Srie if ((sym->st_value < STO_SPARC_REGISTER_G1) ||
1765aefb655Srie (sym->st_value > STO_SPARC_REGISTER_G7)) {
177*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_BADREG),
1785aefb655Srie ifl->ifl_name, symsecname, symndx, EC_XWORD(sym->st_value));
1795aefb655Srie return ((const char *)S_ERROR);
1805aefb655Srie }
1815aefb655Srie
1825aefb655Srie /*
1835aefb655Srie * A register symbol can only be undefined or defined (absolute).
1845aefb655Srie */
1855aefb655Srie if ((shndx != SHN_ABS) && (shndx != SHN_UNDEF)) {
186*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_BADREG),
1875aefb655Srie ifl->ifl_name, symsecname, symndx, EC_XWORD(sym->st_value));
1885aefb655Srie return ((const char *)S_ERROR);
1895aefb655Srie }
1905aefb655Srie
1915aefb655Srie /*
1925aefb655Srie * Determine whether this is a scratch (unnamed) definition.
1935aefb655Srie */
1945aefb655Srie if (sym->st_name == 0) {
1955aefb655Srie /*
1965aefb655Srie * Check for bogus scratch register definitions.
1975aefb655Srie */
1985aefb655Srie if ((ELF_ST_BIND(sym->st_info) != STB_GLOBAL) ||
1995aefb655Srie (shndx != SHN_UNDEF)) {
200de777a60Sab196087 Conv_inv_buf_t inv_buf;
201de777a60Sab196087
202*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_BADSCRATCH),
2035aefb655Srie ifl->ifl_name, symsecname, symndx,
204de777a60Sab196087 conv_sym_SPARC_value(sym->st_value, 0, &inv_buf));
2055aefb655Srie return ((const char *)S_ERROR);
2065aefb655Srie }
2075aefb655Srie
2085aefb655Srie /*
2095aefb655Srie * Fabricate a name for this register so that this definition
2105aefb655Srie * can be processed through the symbol resolution engine.
2115aefb655Srie */
2125aefb655Srie name = registers[sym->st_value];
2135aefb655Srie } else
2145aefb655Srie name = strs + sym->st_name;
2155aefb655Srie
2165aefb655Srie /*
2175aefb655Srie * Indicate we're dealing with a register and return its name.
2185aefb655Srie */
2195aefb655Srie *flags |= FLG_SY_REGSYM;
2205aefb655Srie return (name);
2215aefb655Srie }
2225aefb655Srie
2235aefb655Srie Sym_desc *
ld_reg_find_sparc(Sym * sym,Ofl_desc * ofl)224ba2be530Sab196087 ld_reg_find_sparc(Sym *sym, Ofl_desc *ofl)
2255aefb655Srie {
226635216b6SRod Evans if (ofl->ofl_regsyms == NULL)
227635216b6SRod Evans return (NULL);
2285aefb655Srie
2295aefb655Srie return (ofl->ofl_regsyms[sym->st_value]);
2305aefb655Srie }
2315aefb655Srie
2325aefb655Srie int
ld_reg_enter_sparc(Sym_desc * sdp,Ofl_desc * ofl)233ba2be530Sab196087 ld_reg_enter_sparc(Sym_desc *sdp, Ofl_desc *ofl)
2345aefb655Srie {
235635216b6SRod Evans if (ofl->ofl_regsyms == NULL) {
236635216b6SRod Evans
2375aefb655Srie ofl->ofl_regsymsno = STO_SPARC_REGISTER_G7 + 1;
238635216b6SRod Evans
2395aefb655Srie if ((ofl->ofl_regsyms = libld_calloc(sizeof (Sym_desc *),
240635216b6SRod Evans ofl->ofl_regsymsno)) == NULL) {
2415aefb655Srie ofl->ofl_flags |= FLG_OF_FATAL;
2425aefb655Srie return (0);
2435aefb655Srie }
2445aefb655Srie }
2455aefb655Srie
2465aefb655Srie ofl->ofl_regsyms[sdp->sd_sym->st_value] = sdp;
2475aefb655Srie return (1);
2485aefb655Srie }
249