xref: /illumos-gate/usr/src/cmd/sgs/liblddbg/common/got.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<stdlib.h>
29 #include	"_debug.h"
30 #include	"msg.h"
31 #include	"libld.h"
32 
33 
34 static int
35 Dbg_got_compare(Gottable *gtp1, Gottable *gtp2)
36 {
37 	Gotndx	*gnp1 = &gtp1->gt_gndx;
38 	Gotndx	*gnp2 = &gtp2->gt_gndx;
39 
40 	if (gnp1->gn_gotndx > gnp2->gn_gotndx)
41 		return (1);
42 	if (gnp1->gn_gotndx < gnp2->gn_gotndx)
43 		return (-1);
44 
45 	return (0);
46 }
47 
48 void
49 Dbg_got_display(Ofl_desc *ofl, Gottable *gtp)
50 {
51 	Lm_list	*lml = ofl->ofl_lml;
52 	Word	gotndx;
53 
54 	if (DBG_NOTCLASS(DBG_C_GOT))
55 		return;
56 
57 	if (ofl->ofl_gotcnt == M_GOT_XNumber)
58 		return;
59 
60 	Dbg_util_nl(lml, DBG_NL_STD);
61 	dbg_print(lml, MSG_INTL(MSG_GOT_INFO), EC_WORD(ofl->ofl_gotcnt));
62 
63 	if (DBG_NOTDETAIL())
64 		return;
65 
66 	qsort((char *)gtp, ofl->ofl_gotcnt, sizeof (Gottable),
67 	    (int(*)(const void *, const void *))Dbg_got_compare);
68 
69 	dbg_print(lml, MSG_ORIG(MSG_GOT_COLUMNS));
70 
71 	for (gotndx = 0; gotndx < ofl->ofl_gotcnt; gotndx++, gtp++) {
72 		Sym_desc	*sdp;
73 		const char	*refstr, *name;
74 		Gotndx		*gnp = &gtp->gt_gndx;
75 
76 		if ((sdp = gtp->gt_sym) == 0)
77 			continue;
78 
79 		if (sdp->sd_flags & FLG_SY_SMGOT)
80 			refstr = MSG_ORIG(MSG_GOT_SMALL_PIC);
81 		else
82 			refstr = MSG_ORIG(MSG_GOT_BIG_PIC);
83 
84 		if (sdp->sd_name)
85 			name = Dbg_demangle_name(sdp->sd_name);
86 		else
87 			name = MSG_INTL(MSG_STR_UNKNOWN);
88 
89 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) ||
90 		    (sdp->sd_file == 0)) {
91 			dbg_print(lml, MSG_ORIG(MSG_GOT_FORMAT1),
92 			    EC_SWORD(gnp->gn_gotndx), refstr,
93 			    EC_LWORD(gnp->gn_addend), name);
94 		} else {
95 			dbg_print(lml, MSG_ORIG(MSG_GOT_FORMAT2),
96 			    EC_SWORD(gnp->gn_gotndx), refstr,
97 			    EC_LWORD(gnp->gn_addend),
98 			    sdp->sd_file->ifl_name, name);
99 		}
100 	}
101 }
102 
103 void
104 Elf_got_title(Lm_list *lml)
105 {
106 	dbg_print(lml, MSG_INTL(MSG_GOT_TITLE));
107 }
108 
109 void
110 Elf_got_entry(Lm_list *lml, Sword ndx, Addr addr, Xword value, Half mach,
111     Word type, void *reloc, const char *name)
112 {
113 	Rela		*rela;
114 	Rel		*rel;
115 	const char	*str;
116 	char		index[INDEX_STR_SIZE];
117 
118 	(void) snprintf(index, INDEX_STR_SIZE, MSG_ORIG(MSG_GOT_INDEX),
119 	    EC_SWORD(ndx));
120 
121 	if (reloc) {
122 		if (type == SHT_RELA) {
123 			rela = (Rela *)reloc;
124 			str = conv_reloc_type(mach, ELF_R_TYPE(rela->r_info));
125 		} else {
126 			rel = (Rel *)reloc;
127 			str = conv_reloc_type(mach, ELF_R_TYPE(rel->r_info));
128 		}
129 
130 		if (name)
131 			name = Elf_demangle_name(name);
132 		else
133 			name = MSG_ORIG(MSG_STR_EMPTY);
134 
135 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_RE), index, EC_ADDR(addr),
136 		    EC_XWORD(value), str, name);
137 	} else
138 		dbg_print(lml, MSG_INTL(MSG_GOT_ENTRY_NR), index, EC_ADDR(addr),
139 		    EC_XWORD(value));
140 }
141