xref: /illumos-gate/usr/src/cmd/sgs/liblddbg/common/got.c (revision 2a8bcb4e)
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*ba2be530Sab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
245aefb655Srie  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27a194faf8Srie #include	<stdio.h>
287c478bd9Sstevel@tonic-gate #include	"_debug.h"
297c478bd9Sstevel@tonic-gate #include	"msg.h"
307c478bd9Sstevel@tonic-gate #include	"libld.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static int
Dbg_got_compare(Gottable * gtp1,Gottable * gtp2)345aefb655Srie Dbg_got_compare(Gottable *gtp1, Gottable *gtp2)
357c478bd9Sstevel@tonic-gate {
365aefb655Srie 	Gotndx	*gnp1 = &gtp1->gt_gndx;
375aefb655Srie 	Gotndx	*gnp2 = &gtp2->gt_gndx;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	if (gnp1->gn_gotndx > gnp2->gn_gotndx)
407c478bd9Sstevel@tonic-gate 		return (1);
417c478bd9Sstevel@tonic-gate 	if (gnp1->gn_gotndx < gnp2->gn_gotndx)
427c478bd9Sstevel@tonic-gate 		return (-1);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	return (0);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void
Dbg_got_display(Ofl_desc * ofl,Off goff,int stage,Word m_got_xnumber,size_t m_got_entsize)48*ba2be530Sab Dbg_got_display(Ofl_desc *ofl, Off goff, int stage,
49*ba2be530Sab     Word m_got_xnumber, size_t m_got_entsize)
507c478bd9Sstevel@tonic-gate {
51d326b23bSrie 	Lm_list		*lml = ofl->ofl_lml;
52d326b23bSrie 	Gottable	*gtp = ofl->ofl_gottable;
53d326b23bSrie 	Word		gotndx;
54d326b23bSrie 	Xword		*gptr;
557c478bd9Sstevel@tonic-gate 
565aefb655Srie 	if (DBG_NOTCLASS(DBG_C_GOT))
577c478bd9Sstevel@tonic-gate 		return;
587c478bd9Sstevel@tonic-gate 
59*ba2be530Sab 	if (ofl->ofl_gotcnt == m_got_xnumber)
607c478bd9Sstevel@tonic-gate 		return;
617c478bd9Sstevel@tonic-gate 
625aefb655Srie 	Dbg_util_nl(lml, DBG_NL_STD);
635aefb655Srie 	dbg_print(lml, MSG_INTL(MSG_GOT_INFO), EC_WORD(ofl->ofl_gotcnt));
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (DBG_NOTDETAIL())
667c478bd9Sstevel@tonic-gate 		return;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	qsort((char *)gtp, ofl->ofl_gotcnt, sizeof (Gottable),
695aefb655Srie 	    (int(*)(const void *, const void *))Dbg_got_compare);
707c478bd9Sstevel@tonic-gate 
71d326b23bSrie 	if (stage == 0)
72d326b23bSrie 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS1));
73d326b23bSrie 	else
74d326b23bSrie 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS2));
757c478bd9Sstevel@tonic-gate 
76d326b23bSrie 	gptr = (Xword *)ofl->ofl_osgot->os_outdata->d_buf;
77d326b23bSrie 
78d326b23bSrie 	for (gotndx = 0; gotndx < ofl->ofl_gotcnt; gotndx++, gtp++, gptr++) {
79d326b23bSrie 		Sym_desc	*sdp = gtp->gt_sym;
807c478bd9Sstevel@tonic-gate 		const char	*refstr, *name;
817c478bd9Sstevel@tonic-gate 		Gotndx		*gnp = &gtp->gt_gndx;
82d326b23bSrie 		Lword		gotaddval;
83*ba2be530Sab 		Off		off = goff + (gotndx * m_got_entsize);
84d326b23bSrie 		char		index[INDEX_STR_SIZE];
857c478bd9Sstevel@tonic-gate 
86d326b23bSrie 		(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
87d326b23bSrie 		    EC_SWORD(gnp->gn_gotndx));
887c478bd9Sstevel@tonic-gate 
89d326b23bSrie 		if (sdp == 0)
90d326b23bSrie 			refstr = MSG_ORIG(MSG_STR_EMPTY);
91d326b23bSrie 		else if (sdp->sd_flags & FLG_SY_SMGOT)
927c478bd9Sstevel@tonic-gate 			refstr = MSG_ORIG(MSG_GOT_SMALL_PIC);
937c478bd9Sstevel@tonic-gate 		else
945aefb655Srie 			refstr = MSG_ORIG(MSG_GOT_BIG_PIC);
957c478bd9Sstevel@tonic-gate 
96d326b23bSrie 		if (sdp == 0)
97d326b23bSrie 			name = MSG_ORIG(MSG_STR_EMPTY);
98d326b23bSrie 		else if (sdp->sd_name)
995aefb655Srie 			name = Dbg_demangle_name(sdp->sd_name);
1007c478bd9Sstevel@tonic-gate 		else
1017c478bd9Sstevel@tonic-gate 			name = MSG_INTL(MSG_STR_UNKNOWN);
1027c478bd9Sstevel@tonic-gate 
103d326b23bSrie 		if (stage == 0)
104d326b23bSrie 			gotaddval = gnp->gn_addend;
105d326b23bSrie 		else
106d326b23bSrie 			gotaddval = *gptr;
107d326b23bSrie 
108d326b23bSrie 		if ((sdp == 0) || (sdp->sd_sym->st_shndx == SHN_UNDEF) ||
1097c478bd9Sstevel@tonic-gate 		    (sdp->sd_file == 0)) {
110d326b23bSrie 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT1), index,
111d326b23bSrie 			    refstr, EC_OFF(off), EC_XWORD(gotaddval), name);
1127c478bd9Sstevel@tonic-gate 		} else {
113d326b23bSrie 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT2), index,
114d326b23bSrie 			    refstr, EC_OFF(off), EC_XWORD(gotaddval),
1157c478bd9Sstevel@tonic-gate 			    sdp->sd_file->ifl_name, name);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate void
Elf_got_title(Lm_list * lml)1215aefb655Srie Elf_got_title(Lm_list *lml)
1227c478bd9Sstevel@tonic-gate {
1235aefb655Srie 	dbg_print(lml, MSG_INTL(MSG_GOT_TITLE));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@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)1275aefb655Srie Elf_got_entry(Lm_list *lml, Sword ndx, Addr addr, Xword value, Half mach,
128*ba2be530Sab     uchar_t ei_target_data, uchar_t ei_host_data, Word type, void *reloc,
129*ba2be530Sab     const char *name)
1307c478bd9Sstevel@tonic-gate {
1315aefb655Srie 	Rela		*rela;
1325aefb655Srie 	Rel		*rel;
1335aefb655Srie 	const char	*str;
134de777a60Sab 	Conv_inv_buf_t	inv_buf;
1355aefb655Srie 	char		index[INDEX_STR_SIZE];
1365aefb655Srie 
1375aefb655Srie 	(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
1385aefb655Srie 	    EC_SWORD(ndx));
1395aefb655Srie 
140*ba2be530Sab 	/*
141*ba2be530Sab 	 * Got sections are SHT_PROGBITS, and are therefore not xlated by
142*ba2be530Sab 	 * libelf. If the target system has a different byte order than
143*ba2be530Sab 	 * the system displaying the data, swap the bytes so they are
144*ba2be530Sab 	 * presented properly.
145*ba2be530Sab 	 */
146*ba2be530Sab 	if (ei_target_data != ei_host_data)
147*ba2be530Sab 		value = BSWAP_XWORD(value);
148*ba2be530Sab 
1495aefb655Srie 	if (reloc) {
1505aefb655Srie 		if (type == SHT_RELA) {
1515aefb655Srie 			rela = (Rela *)reloc;
152*ba2be530Sab 			str = conv_reloc_type(mach,
153*ba2be530Sab 			    ELF_R_TYPE(rela->r_info, mach), 0, &inv_buf);
1547c478bd9Sstevel@tonic-gate 		} else {
1555aefb655Srie 			rel = (Rel *)reloc;
156*ba2be530Sab 			str = conv_reloc_type(mach,
157*ba2be530Sab 			    ELF_R_TYPE(rel->r_info, mach), 0, &inv_buf);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 
1605aefb655Srie 		if (name)
1615aefb655Srie 			name = Elf_demangle_name(name);
1625aefb655Srie 		else
1635aefb655Srie 			name = MSG_ORIG(MSG_STR_EMPTY);
1647c478bd9Sstevel@tonic-gate 
1655aefb655Srie 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_RE), index, EC_ADDR(addr),
1665aefb655Srie 		    EC_XWORD(value), str, name);
1675aefb655Srie 	} else
1685aefb655Srie 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_NR), index, EC_ADDR(addr),
1695aefb655Srie 		    EC_XWORD(value));
1707c478bd9Sstevel@tonic-gate }
171