xref: /titanic_41/usr/src/cmd/sgs/liblddbg/common/got.c (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * 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.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*ba2be530Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
28a194faf8Srie #include	<stdio.h>
297c478bd9Sstevel@tonic-gate #include	"_debug.h"
307c478bd9Sstevel@tonic-gate #include	"msg.h"
317c478bd9Sstevel@tonic-gate #include	"libld.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static int
Dbg_got_compare(Gottable * gtp1,Gottable * gtp2)355aefb655Srie Dbg_got_compare(Gottable *gtp1, Gottable *gtp2)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	Gotndx	*gnp1 = &gtp1->gt_gndx;
387c478bd9Sstevel@tonic-gate 	Gotndx	*gnp2 = &gtp2->gt_gndx;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if (gnp1->gn_gotndx > gnp2->gn_gotndx)
417c478bd9Sstevel@tonic-gate 		return (1);
427c478bd9Sstevel@tonic-gate 	if (gnp1->gn_gotndx < gnp2->gn_gotndx)
437c478bd9Sstevel@tonic-gate 		return (-1);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	return (0);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate void
Dbg_got_display(Ofl_desc * ofl,Off goff,int stage,Word m_got_xnumber,size_t m_got_entsize)49*ba2be530Sab196087 Dbg_got_display(Ofl_desc *ofl, Off goff, int stage,
50*ba2be530Sab196087     Word m_got_xnumber, size_t m_got_entsize)
517c478bd9Sstevel@tonic-gate {
525aefb655Srie 	Lm_list		*lml = ofl->ofl_lml;
53d326b23bSrie 	Gottable	*gtp = ofl->ofl_gottable;
547c478bd9Sstevel@tonic-gate 	Word		gotndx;
55d326b23bSrie 	Xword		*gptr;
567c478bd9Sstevel@tonic-gate 
575aefb655Srie 	if (DBG_NOTCLASS(DBG_C_GOT))
587c478bd9Sstevel@tonic-gate 		return;
597c478bd9Sstevel@tonic-gate 
60*ba2be530Sab196087 	if (ofl->ofl_gotcnt == m_got_xnumber)
617c478bd9Sstevel@tonic-gate 		return;
627c478bd9Sstevel@tonic-gate 
635aefb655Srie 	Dbg_util_nl(lml, DBG_NL_STD);
645aefb655Srie 	dbg_print(lml, MSG_INTL(MSG_GOT_INFO), EC_WORD(ofl->ofl_gotcnt));
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (DBG_NOTDETAIL())
677c478bd9Sstevel@tonic-gate 		return;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	qsort((char *)gtp, ofl->ofl_gotcnt, sizeof (Gottable),
705aefb655Srie 	    (int(*)(const void *, const void *))Dbg_got_compare);
717c478bd9Sstevel@tonic-gate 
72d326b23bSrie 	if (stage == 0)
73d326b23bSrie 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS1));
74d326b23bSrie 	else
75d326b23bSrie 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS2));
767c478bd9Sstevel@tonic-gate 
77d326b23bSrie 	gptr = (Xword *)ofl->ofl_osgot->os_outdata->d_buf;
78d326b23bSrie 
79d326b23bSrie 	for (gotndx = 0; gotndx < ofl->ofl_gotcnt; gotndx++, gtp++, gptr++) {
80d326b23bSrie 		Sym_desc	*sdp = gtp->gt_sym;
817c478bd9Sstevel@tonic-gate 		const char	*refstr, *name;
827c478bd9Sstevel@tonic-gate 		Gotndx		*gnp = &gtp->gt_gndx;
83d326b23bSrie 		Lword		gotaddval;
84*ba2be530Sab196087 		Off		off = goff + (gotndx * m_got_entsize);
85d326b23bSrie 		char		index[INDEX_STR_SIZE];
867c478bd9Sstevel@tonic-gate 
87d326b23bSrie 		(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
88d326b23bSrie 		    EC_SWORD(gnp->gn_gotndx));
897c478bd9Sstevel@tonic-gate 
90d326b23bSrie 		if (sdp == 0)
91d326b23bSrie 			refstr = MSG_ORIG(MSG_STR_EMPTY);
92d326b23bSrie 		else if (sdp->sd_flags & FLG_SY_SMGOT)
937c478bd9Sstevel@tonic-gate 			refstr = MSG_ORIG(MSG_GOT_SMALL_PIC);
947c478bd9Sstevel@tonic-gate 		else
955aefb655Srie 			refstr = MSG_ORIG(MSG_GOT_BIG_PIC);
967c478bd9Sstevel@tonic-gate 
97d326b23bSrie 		if (sdp == 0)
98d326b23bSrie 			name = MSG_ORIG(MSG_STR_EMPTY);
99d326b23bSrie 		else if (sdp->sd_name)
1005aefb655Srie 			name = Dbg_demangle_name(sdp->sd_name);
1017c478bd9Sstevel@tonic-gate 		else
1027c478bd9Sstevel@tonic-gate 			name = MSG_INTL(MSG_STR_UNKNOWN);
1037c478bd9Sstevel@tonic-gate 
104d326b23bSrie 		if (stage == 0)
105d326b23bSrie 			gotaddval = gnp->gn_addend;
106d326b23bSrie 		else
107d326b23bSrie 			gotaddval = *gptr;
108d326b23bSrie 
109d326b23bSrie 		if ((sdp == 0) || (sdp->sd_sym->st_shndx == SHN_UNDEF) ||
1107c478bd9Sstevel@tonic-gate 		    (sdp->sd_file == 0)) {
111d326b23bSrie 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT1), index,
112d326b23bSrie 			    refstr, EC_OFF(off), EC_XWORD(gotaddval), name);
1137c478bd9Sstevel@tonic-gate 		} else {
114d326b23bSrie 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT2), index,
115d326b23bSrie 			    refstr, EC_OFF(off), EC_XWORD(gotaddval),
1167c478bd9Sstevel@tonic-gate 			    sdp->sd_file->ifl_name, name);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
Elf_got_title(Lm_list * lml)1225aefb655Srie Elf_got_title(Lm_list *lml)
1237c478bd9Sstevel@tonic-gate {
1245aefb655Srie 	dbg_print(lml, MSG_INTL(MSG_GOT_TITLE));
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate void
Elf_got_entry(Lm_list * lml,Sword ndx,Addr addr,Xword value,Half mach,uchar_t ei_target_data,uchar_t ei_host_data,Word type,void * reloc,const char * name)1285aefb655Srie Elf_got_entry(Lm_list *lml, Sword ndx, Addr addr, Xword value, Half mach,
129*ba2be530Sab196087     uchar_t ei_target_data, uchar_t ei_host_data, Word type, void *reloc,
130*ba2be530Sab196087     const char *name)
1317c478bd9Sstevel@tonic-gate {
1325aefb655Srie 	Rela		*rela;
1335aefb655Srie 	Rel		*rel;
1345aefb655Srie 	const char	*str;
135de777a60Sab196087 	Conv_inv_buf_t	inv_buf;
1365aefb655Srie 	char		index[INDEX_STR_SIZE];
1377c478bd9Sstevel@tonic-gate 
1385aefb655Srie 	(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
1395aefb655Srie 	    EC_SWORD(ndx));
1405aefb655Srie 
141*ba2be530Sab196087 	/*
142*ba2be530Sab196087 	 * Got sections are SHT_PROGBITS, and are therefore not xlated by
143*ba2be530Sab196087 	 * libelf. If the target system has a different byte order than
144*ba2be530Sab196087 	 * the system displaying the data, swap the bytes so they are
145*ba2be530Sab196087 	 * presented properly.
146*ba2be530Sab196087 	 */
147*ba2be530Sab196087 	if (ei_target_data != ei_host_data)
148*ba2be530Sab196087 		value = BSWAP_XWORD(value);
149*ba2be530Sab196087 
1505aefb655Srie 	if (reloc) {
1515aefb655Srie 		if (type == SHT_RELA) {
1525aefb655Srie 			rela = (Rela *)reloc;
153*ba2be530Sab196087 			str = conv_reloc_type(mach,
154*ba2be530Sab196087 			    ELF_R_TYPE(rela->r_info, mach), 0, &inv_buf);
1557c478bd9Sstevel@tonic-gate 		} else {
1565aefb655Srie 			rel = (Rel *)reloc;
157*ba2be530Sab196087 			str = conv_reloc_type(mach,
158*ba2be530Sab196087 			    ELF_R_TYPE(rel->r_info, mach), 0, &inv_buf);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 
1615aefb655Srie 		if (name)
1625aefb655Srie 			name = Elf_demangle_name(name);
1637c478bd9Sstevel@tonic-gate 		else
1645aefb655Srie 			name = MSG_ORIG(MSG_STR_EMPTY);
1657c478bd9Sstevel@tonic-gate 
1665aefb655Srie 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_RE), index, EC_ADDR(addr),
1675aefb655Srie 		    EC_XWORD(value), str, name);
1685aefb655Srie 	} else
1695aefb655Srie 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_NR), index, EC_ADDR(addr),
1705aefb655Srie 		    EC_XWORD(value));
1717c478bd9Sstevel@tonic-gate }
172