xref: /illumos-gate/usr/src/cmd/sgs/liblddbg/common/got.c (revision 2a8bcb4e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<stdio.h>
28 #include	"_debug.h"
29 #include	"msg.h"
30 #include	"libld.h"
31 
32 
33 static int
Dbg_got_compare(Gottable * gtp1,Gottable * gtp2)34 Dbg_got_compare(Gottable *gtp1, Gottable *gtp2)
35 {
36 	Gotndx	*gnp1 = &gtp1->gt_gndx;
37 	Gotndx	*gnp2 = &gtp2->gt_gndx;
38 
39 	if (gnp1->gn_gotndx > gnp2->gn_gotndx)
40 		return (1);
41 	if (gnp1->gn_gotndx < gnp2->gn_gotndx)
42 		return (-1);
43 
44 	return (0);
45 }
46 
47 void
Dbg_got_display(Ofl_desc * ofl,Off goff,int stage,Word m_got_xnumber,size_t m_got_entsize)48 Dbg_got_display(Ofl_desc *ofl, Off goff, int stage,
49     Word m_got_xnumber, size_t m_got_entsize)
50 {
51 	Lm_list		*lml = ofl->ofl_lml;
52 	Gottable	*gtp = ofl->ofl_gottable;
53 	Word		gotndx;
54 	Xword		*gptr;
55 
56 	if (DBG_NOTCLASS(DBG_C_GOT))
57 		return;
58 
59 	if (ofl->ofl_gotcnt == m_got_xnumber)
60 		return;
61 
62 	Dbg_util_nl(lml, DBG_NL_STD);
63 	dbg_print(lml, MSG_INTL(MSG_GOT_INFO), EC_WORD(ofl->ofl_gotcnt));
64 
65 	if (DBG_NOTDETAIL())
66 		return;
67 
68 	qsort((char *)gtp, ofl->ofl_gotcnt, sizeof (Gottable),
69 	    (int(*)(const void *, const void *))Dbg_got_compare);
70 
71 	if (stage == 0)
72 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS1));
73 	else
74 		dbg_print(lml, MSG_INTL(MSG_GOT_COLUMNS2));
75 
76 	gptr = (Xword *)ofl->ofl_osgot->os_outdata->d_buf;
77 
78 	for (gotndx = 0; gotndx < ofl->ofl_gotcnt; gotndx++, gtp++, gptr++) {
79 		Sym_desc	*sdp = gtp->gt_sym;
80 		const char	*refstr, *name;
81 		Gotndx		*gnp = &gtp->gt_gndx;
82 		Lword		gotaddval;
83 		Off		off = goff + (gotndx * m_got_entsize);
84 		char		index[INDEX_STR_SIZE];
85 
86 		(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
87 		    EC_SWORD(gnp->gn_gotndx));
88 
89 		if (sdp == 0)
90 			refstr = MSG_ORIG(MSG_STR_EMPTY);
91 		else if (sdp->sd_flags & FLG_SY_SMGOT)
92 			refstr = MSG_ORIG(MSG_GOT_SMALL_PIC);
93 		else
94 			refstr = MSG_ORIG(MSG_GOT_BIG_PIC);
95 
96 		if (sdp == 0)
97 			name = MSG_ORIG(MSG_STR_EMPTY);
98 		else if (sdp->sd_name)
99 			name = Dbg_demangle_name(sdp->sd_name);
100 		else
101 			name = MSG_INTL(MSG_STR_UNKNOWN);
102 
103 		if (stage == 0)
104 			gotaddval = gnp->gn_addend;
105 		else
106 			gotaddval = *gptr;
107 
108 		if ((sdp == 0) || (sdp->sd_sym->st_shndx == SHN_UNDEF) ||
109 		    (sdp->sd_file == 0)) {
110 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT1), index,
111 			    refstr, EC_OFF(off), EC_XWORD(gotaddval), name);
112 		} else {
113 			dbg_print(lml, MSG_INTL(MSG_GOT_FORMAT2), index,
114 			    refstr, EC_OFF(off), EC_XWORD(gotaddval),
115 			    sdp->sd_file->ifl_name, name);
116 		}
117 	}
118 }
119 
120 void
Elf_got_title(Lm_list * lml)121 Elf_got_title(Lm_list *lml)
122 {
123 	dbg_print(lml, MSG_INTL(MSG_GOT_TITLE));
124 }
125 
126 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)127 Elf_got_entry(Lm_list *lml, Sword ndx, Addr addr, Xword value, Half mach,
128     uchar_t ei_target_data, uchar_t ei_host_data, Word type, void *reloc,
129     const char *name)
130 {
131 	Rela		*rela;
132 	Rel		*rel;
133 	const char	*str;
134 	Conv_inv_buf_t	inv_buf;
135 	char		index[INDEX_STR_SIZE];
136 
137 	(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
138 	    EC_SWORD(ndx));
139 
140 	/*
141 	 * Got sections are SHT_PROGBITS, and are therefore not xlated by
142 	 * libelf. If the target system has a different byte order than
143 	 * the system displaying the data, swap the bytes so they are
144 	 * presented properly.
145 	 */
146 	if (ei_target_data != ei_host_data)
147 		value = BSWAP_XWORD(value);
148 
149 	if (reloc) {
150 		if (type == SHT_RELA) {
151 			rela = (Rela *)reloc;
152 			str = conv_reloc_type(mach,
153 			    ELF_R_TYPE(rela->r_info, mach), 0, &inv_buf);
154 		} else {
155 			rel = (Rel *)reloc;
156 			str = conv_reloc_type(mach,
157 			    ELF_R_TYPE(rel->r_info, mach), 0, &inv_buf);
158 		}
159 
160 		if (name)
161 			name = Elf_demangle_name(name);
162 		else
163 			name = MSG_ORIG(MSG_STR_EMPTY);
164 
165 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_RE), index, EC_ADDR(addr),
166 		    EC_XWORD(value), str, name);
167 	} else
168 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_NR), index, EC_ADDR(addr),
169 		    EC_XWORD(value));
170 }
171