xref: /netbsd-src/external/cddl/osnet/dist/tools/ctf/cvt/fixup_tdescs.c (revision 80e58ff06cba9ecd5cd4d215870d872cac25f6bd)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Workarounds for stabs generation bugs in the compiler and general needed
30  * fixups.
31  */
32 
33 #if HAVE_NBTOOL_CONFIG_H
34 # include "nbtool_config.h"
35 #endif
36 
37 #include <stdio.h>
38 #include <strings.h>
39 
40 #include "ctf_headers.h"
41 #include "ctftools.h"
42 #include "hash.h"
43 #include "memory.h"
44 #include "traverse.h"
45 
46 /*
47  * Due to 4432619, the 6.1 compiler will sometimes incorrectly generate pointer
48  * stabs.  Given a struct foo, and a corresponding typedef struct foo foo_t.
49  * In some cases, when faced with a pointer to a foo_t, the compiler will
50  * sometimes generate a stab that describes a pointer to a struct foo.
51  * Regardless of correctness, this breaks merges, as it occurs inconsistently
52  * by file.  The following two routines know how to recognize and repair foo_t *
53  * and foo_t ** bugs in a specific set of cases.  There is no general way to
54  * solve this problem without a fix to the compiler.  In general, cases should
55  * only be added to these routines to fix merging problems in genunix.
56  */
57 static void
fix_ptrptr_to_struct(tdata_t * td)58 fix_ptrptr_to_struct(tdata_t *td)
59 {
60 	const char *strs[2] = { "as", "fdbuffer" };
61 	const char *mems[2] = { "a_objectdir", "fd_shadow" };
62 	const char *acts[2] = { "vnode", "page" };
63 	const char *tgts[2] = { "vnode_t", "page_t" };
64 	tdesc_t *str;
65 	tdesc_t *act, *tgt;
66 	tdesc_t *p1, *p2;
67 	mlist_t *ml;
68 	int i;
69 
70 	for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) {
71 		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
72 			continue;
73 
74 		for (ml = str->t_members; ml; ml = ml->ml_next) {
75 			if (streq(ml->ml_name, mems[i]))
76 				break;
77 		}
78 		if (!ml)
79 			continue;
80 
81 		if (ml->ml_type->t_type != POINTER || ml->ml_type->t_name ||
82 		    ml->ml_type->t_tdesc->t_type != POINTER ||
83 		    ml->ml_type->t_tdesc->t_name)
84 			continue;
85 
86 		act = ml->ml_type->t_tdesc->t_tdesc;
87 		if (act->t_type != STRUCT || !streq(act->t_name, acts[i]))
88 			continue;
89 
90 		if (!(tgt = lookupname(tgts[i])) || tgt->t_type != TYPEDEF)
91 			continue;
92 
93 		/* We have an instance of the bug */
94 		p2 = xcalloc(sizeof (*p2));
95 		p2->t_type = POINTER;
96 		p2->t_id = td->td_nextid++;
97 		p2->t_tdesc = tgt;
98 
99 		p1 = xcalloc(sizeof (*p1));
100 		p1->t_type = POINTER;
101 		p1->t_id = td->td_nextid++;
102 		p1->t_tdesc = p2;
103 
104 		ml->ml_type = p1;
105 
106 		debug(3, "Fixed %s->%s => ptrptr struct %s bug\n",
107 		    strs[i], mems[i], acts[i]);
108 	}
109 }
110 
111 static void
fix_ptr_to_struct(tdata_t * td)112 fix_ptr_to_struct(tdata_t *td)
113 {
114 	const char *strs[2] = { "vmem", "id_space" };
115 	const char *mems[2] = { NULL, "is_vmem" };
116 	tdesc_t *ptr = NULL;
117 	tdesc_t *str, *vmt;
118 	mlist_t *ml;
119 	int i;
120 
121 	if ((vmt = lookupname("vmem_t")) == NULL || vmt->t_type != TYPEDEF)
122 		return;
123 
124 	for (i = 0; i < (int) (sizeof (strs) / sizeof (strs[0])); i++) {
125 		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
126 			continue;
127 
128 		for (ml = str->t_members; ml; ml = ml->ml_next) {
129 			if (mems[i] && !streq(ml->ml_name, mems[i]))
130 				continue;
131 
132 			if (ml->ml_type->t_type != POINTER ||
133 			    ml->ml_type->t_name ||
134 			    (ml->ml_type->t_tdesc->t_type != STRUCT &&
135 			    ml->ml_type->t_tdesc->t_type != FORWARD) ||
136 			    !streq(ml->ml_type->t_tdesc->t_name, "vmem"))
137 				continue;
138 
139 			debug(3, "Fixed %s->%s => ptr struct vmem bug\n",
140 			    strs[i], ml->ml_name);
141 
142 			if (!ptr) {
143 				ptr = xcalloc(sizeof (*ptr));
144 				ptr->t_type = POINTER;
145 				ptr->t_id = td->td_nextid++;
146 				ptr->t_tdesc = vmt;
147 			}
148 
149 			ml->ml_type = ptr;
150 		}
151 	}
152 }
153 
154 /*
155  * Fix stabs generation bugs.  These routines must be run before the
156  * post-conversion merge
157  */
158 void
cvt_fixstabs(tdata_t * td)159 cvt_fixstabs(tdata_t *td)
160 {
161 	fix_ptrptr_to_struct(td);
162 	fix_ptr_to_struct(td);
163 }
164 
165 struct match {
166 	tdesc_t *m_ret;
167 	const char *m_name;
168 };
169 
170 static int
matching_iidesc(void * arg1,void * arg2)171 matching_iidesc(void *arg1, void *arg2)
172 {
173 	iidesc_t *iidesc = arg1;
174 	struct match *match = arg2;
175 	if (!streq(iidesc->ii_name, match->m_name))
176 		return (0);
177 
178 	if (iidesc->ii_type != II_TYPE && iidesc->ii_type != II_SOU)
179 		return (0);
180 
181 	match->m_ret = iidesc->ii_dtype;
182 	return (-1);
183 }
184 
185 static tdesc_t *
lookup_tdesc(tdata_t * td,char const * name)186 lookup_tdesc(tdata_t *td, char const *name)
187 {
188 	struct match match = { NULL, name };
189 	iter_iidescs_by_name(td, name, matching_iidesc, &match);
190 	return (match.m_ret);
191 }
192 
193 /*
194  * The cpu structure grows, with the addition of a machcpu member, if
195  * _MACHDEP is defined.  This means that, for example, the cpu structure
196  * in unix is different from the cpu structure in genunix.  As one might
197  * expect, this causes merges to fail.  Since everyone indirectly contains
198  * a pointer to a CPU structure, the failed merges can cause massive amounts
199  * of duplication.  In the case of unix uniquifying against genunix, upwards
200  * of 50% of the structures were unmerged due to this problem.  We fix this
201  * by adding a cpu_m member.  If machcpu hasn't been defined in our module,
202  * we make a forward node for it.
203  */
204 static void
fix_small_cpu_struct(tdata_t * td,size_t ptrsize)205 fix_small_cpu_struct(tdata_t *td, size_t ptrsize)
206 {
207 	tdesc_t *cput, *cpu;
208 	tdesc_t *machcpu;
209 	mlist_t *ml, *lml;
210 	mlist_t *cpum;
211 	int foundcpucyc = 0;
212 
213 	/*
214 	 * We're going to take the circuitous route finding the cpu structure,
215 	 * because we want to make sure that we find the right one.  It would
216 	 * be nice if we could verify the header name too.  DWARF might not
217 	 * have the cpu_t, so we let this pass.
218 	 */
219 	if ((cput = lookup_tdesc(td, "cpu_t")) != NULL) {
220 		if (cput->t_type != TYPEDEF)
221 			return;
222 		cpu = cput->t_tdesc;
223 	} else {
224 		cpu = lookup_tdesc(td, "cpu");
225 	}
226 
227 	if (cpu == NULL)
228 		return;
229 
230 	if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT)
231 		return;
232 
233 	for (ml = cpu->t_members, lml = NULL; ml;
234 	    lml = ml, ml = ml->ml_next) {
235 		if (strcmp(ml->ml_name, "cpu_cyclic") == 0)
236 			foundcpucyc = 1;
237 	}
238 
239 	if (foundcpucyc == 0 || lml == NULL ||
240 	    strcmp(lml->ml_name, "cpu_m") == 0)
241 		return;
242 
243 	/*
244 	 * We need to derive the right offset for the fake cpu_m member.  To do
245 	 * that, we require a special unused member to be the last member
246 	 * before the 'cpu_m', that we encode knowledge of here.  ABI alignment
247 	 * on all platforms is such that we only need to add a pointer-size
248 	 * number of bits to get the right offset for cpu_m.  This would most
249 	 * likely break if gcc's -malign-double were ever used, but that option
250 	 * breaks the ABI anyway.
251 	 */
252 	if (!streq(lml->ml_name, "cpu_m_pad") &&
253 	    getenv("CTFCONVERT_PERMISSIVE") == NULL) {
254 		terminate("last cpu_t member before cpu_m is %s; "
255 		    "it must be cpu_m_pad.\n", lml->ml_name);
256 	}
257 
258 	if ((machcpu = lookup_tdesc(td, "machcpu")) == NULL) {
259 		machcpu = xcalloc(sizeof (*machcpu));
260 		machcpu->t_name = xstrdup("machcpu");
261 		machcpu->t_id = td->td_nextid++;
262 		machcpu->t_type = FORWARD;
263 	} else if (machcpu->t_type != STRUCT) {
264 		return;
265 	}
266 
267 	debug(3, "Adding cpu_m machcpu %s to cpu struct\n",
268 	    (machcpu->t_type == FORWARD ? "forward" : "struct"));
269 
270 	cpum = xmalloc(sizeof (*cpum));
271 	cpum->ml_offset = lml->ml_offset + (ptrsize * NBBY);
272 	cpum->ml_size = 0;
273 	cpum->ml_name = xstrdup("cpu_m");
274 	cpum->ml_type = machcpu;
275 	cpum->ml_next = NULL;
276 
277 	lml->ml_next = cpum;
278 }
279 
280 #ifdef __NetBSD__
281 
282 /*
283  * XXX: A crude hack to bring down the number of types for a
284  * GENERIC kernel below 2**15-1 (from ~34000 to ~29800).
285  *
286  * Remove the type attributes "volatile", "const" and "restrict",
287  * for DTRACE these attributes are of little value.
288  */
289 
290 static int
fix_kill_attr_cb(tdesc_t * tdp,tdesc_t ** tdpp,void * private __unused)291 fix_kill_attr_cb(tdesc_t *tdp, tdesc_t **tdpp, void *private __unused)
292 {
293 
294 	while (tdp->t_type == VOLATILE ||
295 	    tdp->t_type == RESTRICT ||
296 	    tdp->t_type == CONST)
297 		tdp = tdp->t_tdesc;
298 
299 	*tdpp = tdp;
300 
301 	return 1;
302 }
303 
304 static tdtrav_cb_f fix_kill_attr_tab[] = {
305 	NULL,
306 	NULL,			/* intrinsic */
307 	NULL,			/* pointer */
308 	NULL,			/* reference */
309 	NULL,			/* array */
310 	NULL,			/* function */
311 	NULL,			/* struct */
312 	NULL,			/* union */
313 	NULL,			/* class */
314 	NULL,			/* enum */
315 	NULL		,	/* forward */
316 	NULL,			/* typedef */
317 	NULL,			/* typedef unres */
318 	fix_kill_attr_cb,	/* volatile */
319 	fix_kill_attr_cb,	/* const */
320 	fix_kill_attr_cb,	/* restrict */
321 };
322 
323 static void
fix_kill_attr(tdata_t * td,size_t ptrsize)324 fix_kill_attr(tdata_t *td, size_t ptrsize)
325 {
326 
327 	(void) iitraverse_hash(td->td_iihash, &td->td_curvgen,
328 	    fix_kill_attr_tab, NULL, NULL, NULL);
329 }
330 
331 #endif /* __NetBSD__ */
332 
333 void
cvt_fixups(tdata_t * td,size_t ptrsize)334 cvt_fixups(tdata_t *td, size_t ptrsize)
335 {
336 	fix_small_cpu_struct(td, ptrsize);
337 #ifdef __NetBSD__
338 	fix_kill_attr(td, ptrsize);
339 #endif
340 }
341