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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"@(#)st_bugs.c	1.5	05/06/08 SMI"
28
29/*
30 * Workarounds for stabs generation bugs in the compiler
31 */
32
33#include <stdio.h>
34#include <strings.h>
35
36#include "ctf_headers.h"
37#include "ctftools.h"
38#include "hash.h"
39#include "memory.h"
40
41/*
42 * Due to 4432619, the 6.1 compiler will sometimes incorrectly generate pointer
43 * stabs.  Given a struct foo, and a corresponding typedef struct foo foo_t.
44 * In some cases, when faced with a pointer to a foo_t, the compiler will
45 * sometimes generate a stab that describes a pointer to a struct foo.
46 * Regardless of correctness, this breaks merges, as it occurs inconsistently
47 * by file.  The following two routines know how to recognize and repair foo_t *
48 * and foo_t ** bugs in a specific set of cases.  There is no general way to
49 * solve this problem without a fix to the compiler.  In general, cases should
50 * only be added to these routines to fix merging problems in genunix.
51 */
52static void
53fix_ptrptr_to_struct(tdata_t *td)
54{
55	char *strs[2] = { "as", "fdbuffer" };
56	char *mems[2] = { "a_objectdir", "fd_shadow" };
57	char *acts[2] = { "vnode", "page" };
58	char *tgts[2] = { "vnode_t", "page_t" };
59	tdesc_t *str;
60	tdesc_t *act, *tgt;
61	tdesc_t *p1, *p2;
62	mlist_t *ml;
63	int i;
64
65	for (i = 0; i < sizeof (strs) / sizeof (strs[0]); i++) {
66		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
67			continue;
68
69		for (ml = str->t_members; ml; ml = ml->ml_next) {
70			if (streq(ml->ml_name, mems[i]))
71				break;
72		}
73		if (!ml)
74			continue;
75
76		if (ml->ml_type->t_type != POINTER || ml->ml_type->t_name ||
77		    ml->ml_type->t_tdesc->t_type != POINTER ||
78		    ml->ml_type->t_tdesc->t_name)
79			continue;
80
81		act = ml->ml_type->t_tdesc->t_tdesc;
82		if (act->t_type != STRUCT || !streq(act->t_name, acts[i]))
83			continue;
84
85		if (!(tgt = lookupname(tgts[i])) || tgt->t_type != TYPEDEF)
86			continue;
87
88		/* We have an instance of the bug */
89		p2 = xcalloc(sizeof (*p2));
90		p2->t_type = POINTER;
91		p2->t_id = td->td_nextid++;
92		p2->t_tdesc = tgt;
93
94		p1 = xcalloc(sizeof (*p1));
95		p1->t_type = POINTER;
96		p1->t_id = td->td_nextid++;
97		p1->t_tdesc = p2;
98
99		ml->ml_type = p1;
100
101		debug(3, "Fixed %s->%s => ptrptr struct %s bug\n",
102		    strs[i], mems[i], acts[i]);
103	}
104}
105
106static void
107fix_ptr_to_struct(tdata_t *td)
108{
109	char *strs[2] = { "vmem", "id_space" };
110	char *mems[2] = { NULL, "is_vmem" };
111	tdesc_t *ptr = NULL;
112	tdesc_t *str, *vmt;
113	mlist_t *ml;
114	int i;
115
116	if ((vmt = lookupname("vmem_t")) == NULL || vmt->t_type != TYPEDEF)
117		return;
118
119	for (i = 0; i < sizeof (strs) / sizeof (strs[0]); i++) {
120		if (!(str = lookupname(strs[i])) || str->t_type != STRUCT)
121			continue;
122
123		for (ml = str->t_members; ml; ml = ml->ml_next) {
124			if (mems[i] && !streq(ml->ml_name, mems[i]))
125				continue;
126
127			if (ml->ml_type->t_type != POINTER ||
128			    ml->ml_type->t_name ||
129			    (ml->ml_type->t_tdesc->t_type != STRUCT &&
130			    ml->ml_type->t_tdesc->t_type != FORWARD) ||
131			    !streq(ml->ml_type->t_tdesc->t_name, "vmem"))
132				continue;
133
134			debug(3, "Fixed %s->%s => ptr struct vmem bug\n",
135			    strs[i], ml->ml_name);
136
137			if (!ptr) {
138				ptr = xcalloc(sizeof (*ptr));
139				ptr->t_type = POINTER;
140				ptr->t_id = td->td_nextid++;
141				ptr->t_tdesc = vmt;
142			}
143
144			ml->ml_type = ptr;
145		}
146	}
147}
148
149/*
150 * The cpu structure grows, with the addition of a machcpu member, if
151 * _MACHDEP is defined.  This means that, for example, the cpu structure
152 * in unix is different from the cpu structure in genunix.  As one might
153 * expect, this causes merges to fail.  Since everyone indirectly contains
154 * a pointer to a CPU structure, the failed merges can cause massive amounts
155 * of duplication.  In the case of unix uniquifying against genunix, upwards
156 * of 50% of the structures were unmerged due to this problem.  We fix this
157 * by adding a cpu_m member.  If machcpu hasn't been defined in our module,
158 * we make a forward node for it.
159 */
160static void
161fix_small_cpu_struct(tdata_t *td)
162{
163	tdesc_t *cput, *cpu;
164	tdesc_t *machcpu;
165	mlist_t *ml, *lml;
166	mlist_t *cpum;
167	int foundcpucyc = 0;
168	size_t lmlsz;
169
170	/*
171	 * We're going to take the circuitous route finding the cpu structure,
172	 * because we want to make sure that we find the right one.  It would
173	 * be nice if we could verify the header name too.
174	 */
175	if ((cput = lookupname("cpu_t")) == NULL || cput->t_type != TYPEDEF)
176		return;
177
178	cpu = cput->t_tdesc;
179	if (!streq(cpu->t_name, "cpu") || cpu->t_type != STRUCT)
180		return;
181
182	for (ml = cpu->t_members, lml = NULL; ml;
183	    lml = ml, ml = ml->ml_next) {
184		if (strcmp(ml->ml_name, "cpu_cyclic") == 0)
185			foundcpucyc = 1;
186	}
187
188	if (foundcpucyc == 0 || lml == NULL ||
189	    strcmp(lml->ml_name, "cpu_m") == 0)
190		return;
191
192	/*
193	 * We need to find out how big the last element is so we can compute the
194	 * offset of the new one.  If the last element has a size, we use it.
195	 * If it doesn't, it should be a pointer, which won't have a size.  In
196	 * that case, we look up the size of a long and use that as the size of
197	 * the pointer.
198	 */
199	if (lml->ml_type->t_size)
200		lmlsz = lml->ml_type->t_size * NBBY;
201	else {
202		tdesc_t *tdp = lookupname("long");
203		lmlsz = tdp->t_intr->intr_nbits;
204	}
205
206	if ((machcpu = lookupname("machcpu")) == NULL) {
207		machcpu = xcalloc(sizeof (*machcpu));
208		machcpu->t_name = xstrdup("machcpu");
209		machcpu->t_id = td->td_nextid++;
210		machcpu->t_type = FORWARD;
211
212	} else if (machcpu->t_type != STRUCT)
213		return;
214
215	debug(3, "Adding cpu_m machcpu %s to cpu struct\n",
216	    (machcpu->t_type == FORWARD ? "forward" : "struct"));
217
218	cpum = xmalloc(sizeof (*cpum));
219	cpum->ml_offset = lml->ml_offset + lmlsz;
220	cpum->ml_size = 0;
221	cpum->ml_name = xstrdup("cpu_m");
222	cpum->ml_type = machcpu;
223	cpum->ml_next = NULL;
224
225	lml->ml_next = cpum;
226}
227
228/*
229 * Fix stabs generation bugs.  These routines must be run before the
230 * post-conversion merge
231 */
232void
233cvt_fixbugs(tdata_t *td)
234{
235	fix_ptrptr_to_struct(td);
236	fix_ptr_to_struct(td);
237	fix_small_cpu_struct(td);
238}
239